An example of reading a text file

The code is very simple. Read instruction is use according to the SCILAb help.

The text file has 2 columns of 31 real numbers. It could be the (x,y) values of a dedicated math function:

0	2.718281828
1	1.648721271
2	1.395612425
3	1.284025417
4	1.221402758
5	1.181360413
6	1.153564995
7	1.133148453
8	1.117519069
9	1.105170918
10	1.09516944
11	1.08690405
12	1.079958999
13	1.074041431
14	1.068939106
15	1.064494459
16	1.060588062
17	1.057127745
18	1.054041243
19	1.051271096
20	1.048771047
21	1.046503435
22	1.044437289
23	1.042546905
24	1.040810774
25	1.039210758
26	1.037731455
27	1.036359701
28	1.035084182
29	1.033895114
30	1.032783996

The SCI file is the following one:

// Reading the txt file---------------------------------------------------------
y=read("C:\...path...\Essai_scilab.txt",31,2)
//31 = number of rows
//2 = number of columns

//Ploting the results-----------------------------------------------------------
clf;
plot(y(:,1),y(:,2),'thickness',2);
xgrid(2,1,8);
xlabel('1st column data');xlabel color blue fontsize 3;//Define x axis
ylabel('2nd column data');ylabel color blue fontsize 3;//Define y axis
title("Reading a text file");title color peru background grey90 fontsize 5;
gcf().background=color('gray');//Set the background color of the figure
gca().background=color('gray85');//Set the background color of the plotting area
// colors names are coming from Color_list in SCILAB help browser

The result is given below:

Hi Pascal77,

You can also try “fscanfMat”

y=fscanfMat(“C:.…path…\Essai_scilab.txt”)

Why? no need to know the size of the matrix to upload

BR
François

1 Like

Hello, thank you for the tip.
I will try it…

I usually use fscanfMat, but it is interesting to know this alternative.
Probably, the format options of read may add some useful features.

1 Like