help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question on fscanf ....


From: Philip Nienhuis
Subject: Re: Question on fscanf ....
Date: Sun, 22 Mar 2020 11:25:54 -0500 (CDT)

nrjank wrote
>> My questions:
>> 1.) is there  any way to circumvent the usage of Y and do some kind of
>> [t,y]=fscanf(infile, "%*s %f %*lf %*f %*f %*f %*f %f %*f %*f", ??????);
> 
> I think you may prefer to use textscan.  This function respects the *
> 'ignore' flag and also stores outputs in separate elements of a cell
> array.
> 
> Eg.,:
> 
>>> foo=fopen('data.dat');
>>> bar = textscan(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f")
> bar =
> {
>   [1,1] =
> 
>      0.00000
>      0.25000
>      0.50000
>      0.75000
>      1.00000
>      1.25000
>      1.50000
>      1.75000
>      2.00000
>      2.25000
> 
>   [1,2] =
> 
>      0
>      0
>      0
>      0
>      0
>      0
>      0
>      0
>      0
>      0
> 
> }
> 
> while these are in a single cell array, they are separately
> addressable elements. It may be possible to get the 'C' style version
> to work, but on a quick try I can only get it to read in the one line
> at a time even if I play with \n's :
>  >> foo = fopen('data.dat');
>>> [bar1,bar2] = fscanf(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f", "C")
> bar1 = 0
> bar2 = 0
>>> [bar1,bar2] = fscanf(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f", "C")
> bar1 =  0.25000
> bar2 = 0
>>> [bar1,bar2] = fscanf(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f", "C")
> bar1 =  0.50000
> bar2 = 0
>>> [bar1,bar2] = fscanf(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f", "C")
> bar1 =  0.75000
> bar2 = 0
>>> [bar1,bar2] = fscanf(foo, "%*s %f %*f %*f %*f %*f %*f %f %*f %*f", "C")
> bar1 =  1
> bar2 = 0

If you insist in doing it in one statement you can brush of textread.m and
ignore the "obsolete" warning:

>> [a, b] = textread ('data.dat',  '%*s %f %*f %*f %*f %*f %*f %f %*f %*f')
warning: textread is obsolete; use textscan instead
a =
        0
   0.2500
   0.5000
   0.7500
   1.0000
   1.2500
   1.5000
   1.7500
   2.0000
   2.2500

b =
   0
   0
   0
   0
   0
   0
   0
   0
   0
   0

textread.m isn't quite as fast as textscan and has some limitations, but
otherwise still works well.

Philip



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]