[Scilab-users] Reading a matrix - variable number of lines, columns

classic Classic list List threaded Threaded
3 messages Options
W.Schulz W.Schulz
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Reading a matrix - variable number of lines, columns

Hello,
I have a file with different matrices inside. This matrices have a line with a certain keyword before the data.
I can read the data with the following code if I have nlat rows and 5 columns.

fid=mopen(datafile,'r');                                   // open the file for reading
    //Skip data till the data part:
    line=mgetl(fid,1);
    while(strcmp(line, " Avg Num Sensors: ")~=0)
        line=mgetl(fid,1);  
    end
    //Read the data
    ANSR=mfscanf(nlat,fid,'%f %f %f %f %f ');
mclose(fid);

My problem is the following: I searched but I didn't find a way to make the number of columns a variable.
I tried to read the data line by line  and wanted to use msscanf to scan the string but that didn't work.
In the following example I tried to analyze 5 values of the string line but it didn't work.
-->line
 line  =
 
        0.0        2.8        5.7        4.2        3.5
a= msscanf(5,line,'%f')

Thanks for your help
Wolfgang


Example data file:
    Stroke Count:
         0         77        246      24937          8
         0       5862       7895        318        300
      2387      15871       1151       3166       2057
      5099       1721          1         27         80

Median Semimajor:
       0.0 >=     0.9        0.3        0.5 >=     0.9
       0.0        0.1        0.2 >=     0.9 >=     0.9
       0.4        0.2        0.3 >=     0.9 >=     0.9
       0.2 >=     0.9        0.1 >=     0.9 >=     0.9

 Avg Num Sensors:
       0.0        2.8        5.7        4.2        3.5
       0.0        7.9        6.6        5.0        5.7
       4.5        6.7        6.5        6.3        7.3
       7.4        5.1        2.0        5.0        5.8
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Stanislav Stanislav
Reply | Threaded
Open this post in threaded view
|

Re: [Scilab-users] Reading a matrix - variable number of lines, columns

Hi.

I do not understand what do you want to get: one matrix of three?
Here is script to get one matrix.
 
read_matrix.sce

'test.txt' is your text file.

Stanislav.
W.Schulz W.Schulz
Reply | Threaded
Open this post in threaded view
|

Re: Reading a matrix - variable number of lines, columns

Hello,
thanks for the hint. I solved it with the following code:

    Stroke_count=zeros(nlat,nlon);  
    for i=1:nlat        
        line=mgetl(fid,1);  
        Stroke_count(i,:)=strtod(tokens(line))';        
    end

Wolfgang

> -----Urspr√ľngliche Nachricht-----
> Von: [hidden email] [mailto:[hidden email]] Im
> Auftrag von Stanislav
> Gesendet: Mittwoch, 21. August 2013 17:40
> An: [hidden email]
> Betreff: Re: [Scilab-users] Reading a matrix - variable number of lines, columns
>
> Hi.
>
> I do not understand what do you want to get: one matrix of three?
> Here is script to get one matrix.
>
> read_matrix.sce
> <http://mailinglists.scilab.org/file/n4027265/read_matrix.sce>
>
> 'test.txt' is your text file.
>
> Stanislav.
>
>
>
> --
> View this message in context: http://mailinglists.scilab.org/Scilab-users-
> Reading-a-matrix-variable-number-of-lines-columns-tp4027264p4027265.html
> Sent from the Scilab users - Mailing Lists Archives mailing list archive at
> Nabble.com.
> _______________________________________________
> users mailing list
> [hidden email]
> http://lists.scilab.org/mailman/listinfo/users
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users