Friday, 23 June 2017

Reading data from a text file

Let us assume that one has a text file "data.txt" with data, for example two columns of numbers corresponding to time and displacement of some real mechanical oscillator, respectively. The file looks as follows:
0.000000000000000E+000     25.6878000000000
1.000000000000001E-002      24.6813600000000
2.000000000000002E-002      21.7823300000000
3.000000000000003E-002      17.2294600000000
4.000000000000004E-002      11.4315700000000
5.000000000000004E-002      4.82601000000000
6.000000000000005E-002      -2.02597000000000
7.000000000000006E-002      -8.59760000000000
...                                               ...
2.95000000000000                  3.100000000000103E-003
2.96000000000000                  2.970000000000139E-003
2.97000000000000                  2.850000000000019E-003
2.98000000000000                  2.970000000000139E-003
2.99000000000000                  3.100000000000103E-003
3.00000000000000                  2.970000000000139E-003



Then one can read and plot the data using the following script "read.sce":
cd('D:\blog\scilab\2017-06-19');   // your current directory
data = fscanfMat('data.txt');          // reads  data
te = data(1:$,1);                             // takes the first column from data            
xe = data(1:$,2);                            // takes the second column from data
clf(1)                                             // clears figure window no. 1
figure(1)                                        // sets the current figure window no. 1
plot(te,xe)                                      // plots  

Then running the above script, you obtain the following plot:

1 comment:

Plotting a function in Scilab

Suppose we want to plot the graph of a function. This can be done as shown in the following script: x = 0 : 0.01 : 15 ; ...