For Programmers: Free Programming Magazines  


Home > Archive > Matlab > April 2005 > How can I check the length of a ASCI file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How can I check the length of a ASCI file
NeeDIX

2005-04-28, 9:00 am

Hi!

I´m using fopen and fread to open and to read my ASCI file, Then I
would like to check a specific column in that file. So how I do that
in the moment is that I need to know how many columns that there is
in that file so when I use fread I can specified a matrix [x y] that
the file is going to have.

But now is there any way that to do this with out knowing anything
from the ASCI file?? e.g. can make a routine that checks how long a
row is and then specified my matrix to be that wide?

Regards
NeeDIX
Lothar Schmidt

2005-04-28, 9:00 am

"NeeDIX" <nikeri02@ryp.umu.se> schrieb im Newsbeitrag
news:ef044fe.-1@webx.raydaftYaTP...
> Hi!
>
> in the moment is that I need to know how many columns that there is
> in that file so when I use fread I can specified a matrix [x y] that
> the file is going to have.
>
> But now is there any way that to do this with out knowing anything
> from the ASCI file?? e.g. can make a routine that checks how long a
> row is and then specified my matrix to be that wide?
>


just an idea:
you could read one line by fgetl (rewind filepointer with fs afteward)
and count the number of separators. That's the number of columns (beware
that a separator might stand in front of the first value and/or behind the
last value in the line).
If you divide the length of your file by the length of the line you'll get a
guess (not more) about the number of rows. [e.g.:
info=dir(filename);bytes_in_file=info.bytes]

regards
Lothar


NeeDIX

2005-04-28, 9:00 am

Lothar Schmidt wrote:
>
>
> "NeeDIX" <nikeri02@ryp.umu.se> schrieb im Newsbeitrag
> news:ef044fe.-1@webx.raydaftYaTP...
there[color=darkred]
> is
> that
> anything
long[color=darkred]
> a
>
> just an idea:
> you could read one line by fgetl (rewind filepointer with fs
> afteward)
> and count the number of separators. That's the number of columns
> (beware
> that a separator might stand in front of the first value and/or
> behind the
> last value in the line).
> If you divide the length of your file by the length of the line
> you'll get a
> guess (not more) about the number of rows. [e.g.:
> info=dir(filename);bytes_in_file=info.bytes]
>
> regards
> Lothar


Hi Lothar!

The number of rows is not inportant, it is just the nr of columns,
and that works just fine... tnx to you.
>
>
>

us

2005-04-28, 9:00 am

NeeDIX:
<SNIP how many cols are in the ascii file...

<lothar schmidt> showed a nice solution
another - maybe slightly more robust one - could be

% assume you have an ascii file with this format, ie,
% two delimiters <,> and <SPACE>
% (note the leading <%>s are NOT in the file)
fnam='foo.txt';
type(fnam)
% 1,11 2,22 3 4,44
% 10,11 20,22 30 40,44

% now
del=', ';
fp=fopen(fnam,'rt');
if fp > 1
s=fread(fp,inf,'*char');
fclose(fp);
s=strread(s,'%s','delimiter','');
% -and-
c=strread(char(s(1,:)).','%s','delimiter',del);
nc=length(c);
end
% results
s
c.'
nc

just a thought
us
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com