[Scilab-users] Read matrix from text file

Tim Wescott tim at wescottdesign.com
Mon Mar 20 17:53:25 CET 2017


On Mon, 2017-03-20 at 17:26 +0100, Frieder Nikolaisen wrote:
> Hello,
> I do try to read a matrix of floating point numbers from a text file.
> I want to have floating point numbers in Sciliab. (I used before
> mgetl which return strings).
> I have attached the example text file and scilab file. The error it
> says is: read: Wrong number of output argument(s): 0 expected.
> Thank you.
> Greetings
> Frieder
>  
> My code:
> Koeffizienten_Pfad = uigetfile(["*.txt"],'',"Wählen Sie die Datei
> Koeffizienten");
> 
> Koeffizienten = mopen(Koeffizienten_Pfad)
> 
> B = read(Koeffizienten,-1,4)
> 
> disp(B)
> 
> mclose('all')

Scilab has multiple contradictory ways of managing files.  It appears
that "mopen" is not compatible with "read".  To use read:

-->file("open", "bob.txt", "old");
 
-->read(bob, -1, 4)
 ans  =
 
    1.    2.    3.    4.  
    5.    6.    7.  - 8.  
 
-->file("close", bob);

However, if you can trust that your file will always be in a tidy
format, you can use fscanfMat:

-->fscanfMat("bob.txt")
 ans  =
 
    1.    2.    3.    4.  
    5.    6.    7.  - 8.

(You can also use mopen and mfscanf -- the "file" stuff is "Fortran-
like", the "mopen" stuff is "C-like", and fscanfMat is just convenient.
 I tend to use the C-like stuff because most of my "real" programming
work is done in C, so I'm familiar with it.  Pick your poison.)

-- 

Tim Wescott
www.wescottdesign.com
Control & Communications systems, circuit & software design.
Phone: 503.631.7815
Cell:  503.349.8432






More information about the users mailing list