textread substitution

classic Classic list List threaded Threaded
6 messages Options
ash
Reply | Threaded
Open this post in threaded view
|

textread substitution

ash
Hi all,

I need to substitute textread function because i hade to use an old version of Octave.
Every guide I read tell the same thing: use fscanf.
But fscanf works differently.
I have a file.txt made in this way

acb
qwertyuiop
mickey
mouse
...

I need to get a vector in wich every cell is an entire row of this file (and not a matrix with a single letter for every cell).
Can you help me ?

Thanks a lor
Reply | Threaded
Open this post in threaded view
|

Re: textread substitution

Ismael Diego Nunez-Riboni
> I need to get a vector in wich every cell is an entire row of this file (and
> not a matrix with a single letter for every cell).
> Can you help me ?

Use fgets and make an array of cells (I'm not running this code, i.e.,
it might have errors):

fid = fopen(yorfile);

linea = 1;

k = 1;
while linea > 0
    linea = fgets(fid); % this is -1 if you reach eof

    if linea > 0
       ll{k} = linea;
    end

    k = k +1;
end

fclose(fid);
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: textread substitution

Philip Nienhuis
In reply to this post by ash
ash wrote
Hi all,

I need to substitute textread function because i hade to use an old version of Octave.
Exactly what version?

Every guide I read tell the same thing: use fscanf.
But fscanf works differently.
I have a file.txt made in this way

acb
qwertyuiop
mickey
mouse
...

I need to get a vector in wich every cell is an entire row of this file (and not a matrix with a single letter for every cell).
Can you help me ?

Thanks a lor
Use something like <very rough):

<pre-allocate cell array, otherwise your code will take forever>
:
fid = fopen (...)
str = '';
while (ischar (str))
  str = fgetl (fid, ....)
  <append str to cell array>
endwhile

See: help fgetl

----or (even uglier)----

<open file>
str = fgets (file, <very_big_number_sufficient_to_read_entire_file>)
vector = strsplit (str, "\n")

See: help fgets

As fgets and fgetl are very similar they might be interchanged here.

(I don't know strsplit's performance but is has been tuned up by one of the devs a while ago so shouldn't be too bad).


Philip
ash
Reply | Threaded
Open this post in threaded view
|

Re: textread substitution

ash
Thanx a lot to everyone.
First I tried with Ismael solution and it works!!

However I'm using 4.2.2.

thanks a lot
Reply | Threaded
Open this post in threaded view
|

Re: textread substitution

c.-2

On 19 Dec 2012, at 17:53, ash wrote:

> However I'm using 4.2.2.
4.2.2 what? the latest Octave release is 3.6.3 ...
c.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
ash
Reply | Threaded
Open this post in threaded view
|

Re: textread substitution

ash
a-ehm...

permutation wrog...

2.4.2 :-)