textread...read first and last row

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

textread...read first and last row

turbofib
This post was updated on .
hi,
look that

01/04/2008 0.00000 0 0.00000 0.00000 0
02/04/2009 -600.00000 0 0.00000 931.25000 1
03/04/2010 750.00000 1 0.00000 1362.50000 2
04/04/2011 12.50000 1 -18.75000 1000.00000 2
05/04/2012 0.00000 0 0.00000 0.00000 2
06/04/2013 -256.25000 1 12.50000 343.75000 2
07/04/2014 -6.25000 1 0.00000 575.00000 2
08/04/2015 -1100.00000 0 12.50000 1556.25000 2

i want catch the first and the last row with textread (i want catch only
date)

the number of lines is not fixed

here i catch the first row (year 2008)
[datInizio]=textread (nomeFileDaEsaminare, "%s ",1);

how can i code to catch the last row?(year 2015)

thank to help me

(i read https://octave.sourceforge.io/octave/function/textread.html but i don't find it)

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


Reply | Threaded
Open this post in threaded view
|

Re: textread...read first and last row

apjanke-floss


On 3/16/19 7:18 AM, turbofib wrote:

> hi,
> look that
>
> 01/04/2008 0.00000 0 0.00000 0.00000 0
> 02/04/2009 -600.00000 0 0.00000 931.25000 1
> 03/04/2010 750.00000 1 0.00000 1362.50000 2
> 04/04/2011 12.50000 1 -18.75000 1000.00000 2
> 05/04/2012 0.00000 0 0.00000 0.00000 2
> 06/04/2013 -256.25000 1 12.50000 343.75000 2
> 07/04/2014 -6.25000 1 0.00000 575.00000 2
> 08/04/2015 -1100.00000 0 12.50000 1556.25000 2
>
> i want catch the first and the last row with textread (i want catch only
> date)
>
> the number of lines is not fixed
>
> here i catch the first row (year 2008)
> [datInizio]=textread (nomeFileDaEsaminare, "%s ",1);
>
> how can i code to catch the last row?(year 2015)
>
> thank to help me

You can use fopen()/fgetl() to do line-oriented input. Something like this:

fid = fopen(nomeFileDaEsaminare);
firstLine = fgetl(fid);
lastLine = [];
while true
   line = fgetl(fid);
   if line == -1
     break
   end
   lastLine = liine;
end
fclose(fid);

You could also use textscan() to read the whole file in tabular format,
and then extract the first and last rows of data. This is probably more
useful and flexible for you.

fid = fopen(nomeDileDaEsaminare
c = textscan(fid, "%s %f %f %f %f %f")
fclose(fid);
firstLineData = c(1,:);
lastLineData = c(end,:);

Cheers,
Andrew