# An example of reading a text file

**URL:** <https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540>\
**Category:** Data handling\
**Created:** [June 5, 2024, 5:55pm UTC](https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540 "2024-06-05T17:55:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [June 5, 2024, 5:55pm UTC](https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540/1 "2024-06-05T17:55:18Z")

</div>

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:

```auto
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:

```auto
// 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:

 ![Reading a txt file with read instruction](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/50d2e5af8c442424a4b6c62cc7ebf69fed025295.png)

---

<div class="post-metadata">

**Author:** ![QwillDune](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/qwilldune/32/152_2.png) [@QwillDune](https://scilab.discourse.group/u/QwillDune)\
**Post date:** [June 6, 2024, 1:09pm UTC](https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540/2 "2024-06-06T13:09:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Pascal77](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@Pascal77](https://scilab.discourse.group/u/Pascal77)\
**Post date:** [June 6, 2024, 1:57pm UTC](https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540/3 "2024-06-06T13:57:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mario](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mario/32/396_2.png) [@mario](https://scilab.discourse.group/u/mario)\
**Post date:** [June 10, 2024, 7:12am UTC](https://scilab.discourse.group/t/an-example-of-reading-a-text-file/540/4 "2024-06-10T07:12:07Z")

</div>

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