Discussion:
textread or textscan without strings
(too old to reply)
spok
2006-05-24 19:18:07 UTC
Permalink
Hello comunauty Matlab.

I've got a little question about reading a file...

does anybody knows how to use the textread or textscan function in
order to extract only numbers without text ??

for my pb, I've got a file from a recording instrument, with a header
which I want to extract the number without the strings...
I'd like to read it systematically without having to specify the
undesirable strings...

basically I'd like: " C = read(file, 'no string')" C being a cell
array.

I went through the textread and textscan manual couple of time, but
can't find any sweet way to do it... (excuse me if I'm blind today, I
just come back the swiming pool...)

anybody with an idea ?

S.
Ri
2006-05-25 04:02:20 UTC
Permalink
Hi

When you have a file which includes following elements,

Sally Type1 12.34 45 Yes
Joe Type2 23.54 60 No
Bill Type1 34.90 12 No

and its name is "Test.txt", then try as follows.

[A,B]=textread('Test.txt','%*s%*s%f%2d%*s')

Then, you got

A =

12.3400
23.5400
34.9000

B =

45
60
12

Please consider the meaning of the option '%*s'.
spok
2006-05-25 14:10:39 UTC
Permalink
Hehe,

I'd tried that already, and it worksfine if you've got the same
"construction" (I mean by that the same number of strings for each
lines),
but my pb is that I don't have the same, ex:

NUMBER OF FREQUENCIES = 129
INITIAL FREQUENCY (Hz) = 0.000
FREQUENCY SPACING (Hz) = 0.005
RESOLVABLE FREQUENCY RANGE (Hz) = 0.070 TO 0.205
NUMBER OF DIRECTIONS = 121
DIRECTION SPACING (DEG) = 3.0
COLUMNS = 0.00 TO 360.00 DEG
ROWS = 0.00 TO 0.64 Hz

and I'd like to extract the numbers one by one in differents arrays, or
cell array...

up to now, I've done it by extracting one line at a time, but I was
wondering if there were not any tricks to handle it more quickly...

S.
spok
2006-05-26 14:42:34 UTC
Permalink
ok, well, I think I'll reply to myself so, if one day any "textread or
textscan newbies" would ever come over here ! :o)

so for my case, and as I had a constant delimiter "=" between my
undesired strings and desired numbers, I could use teh textscan
function as:

fid = fopen(filename)
C = textscan(fid, '%s %f','delimiter','=');
fclose(fid)

However, as my numbers were in some fields mixed to somes strings, I
had to treat them independently as :

C = textscan(fid, 'RESOLVABLE FREQUENCY RANGE (Hz) = %f %*s %*f', 1);
C = textscan(fid, 'RESOLVABLE FREQUENCY RANGE (Hz) = %*f %*s %f', 1);

but it could also have been handled by:

C = textscan(fid, '%*s %*s %*s %*s %*s %f %*s %*f', 1);
C = textscan(fid, '%*s %*s %*s %*s %*s %*f %*s %f', 1);

or by

C = textscan(fid, '%*33c %f %*s %*f', 1);
C = textscan(fid, '%*33c %*f %*s %f', 1);

at the end, and as I needed all my variables in different arrays and
with different names, I finished by adopting that:

nbfreq = textread(filename, 'NUMBER OF FREQUENCIES = %n', 1,
'headerlines', 4);
inifreq = textread(filename, 'INITIAL FREQUENCY (Hz) = %f', 1,
'headerlines', 5);
binfreq = textread(filename, 'FREQUENCY SPACING (Hz) = %f', 1,
'headerlines', 6);
gdfreqmin = textread(filename, 'RESOLVABLE FREQUENCY RANGE (Hz) = %f
%*s %*f', 1, 'headerlines', 7);
gdfreqmax = textread(filename, 'RESOLVABLE FREQUENCY RANGE (Hz) = %*f
%*s %f', 1, 'headerlines', 7);
nbdir = textread(filename, 'NUMBER OF DIRECTIONS = %f', 1,
'headerlines', 8);
bindir = textread(filename, 'DIRECTION SPACING (DEG) = %f', 1,
'headerlines', 9);
mindir = textread(filename, 'COLUMNS = %f%*s%*f%*s', 1,
'headerlines', 10);
maxdir = textread(filename, 'COLUMNS = %*f%*s%f%*s', 1,
'headerlines', 10);
minfreq = textread(filename, 'ROWS = %f%*s%*f%*s', 1, 'headerlines',
11);
maxfreq = textread(filename, 'ROWS = %*f%*s%f%*s', 1, 'headerlines',
11);

with a old textread function ! ;o)

have fun,

S.
Jos
2006-05-26 22:33:46 UTC
Permalink
Post by spok
Hehe,
I'd tried that already, and it worksfine if you've got the same
"construction" (I mean by that the same number of strings for each
lines),
NUMBER OF FREQUENCIES = 129
INITIAL FREQUENCY (Hz) = 0.000
FREQUENCY SPACING (Hz) = 0.005
RESOLVABLE FREQUENCY RANGE (Hz) = 0.070 TO 0.205
NUMBER OF DIRECTIONS = 121
DIRECTION SPACING (DEG) = 3.0
COLUMNS = 0.00 TO 360.00 DEG
ROWS = 0.00 TO 0.64 Hz
and I'd like to extract the numbers one by one in differents
arrays, or
cell array...
up to now, I've done it by extracting one line at a time, but I was
wondering if there were not any tricks to handle it more quickly...
S.
This might do:

s = textread('myfile.txt','%s') ;
s2 = regexprep(s,'[a-z=()]',' ','ignorecase') ;
s2(:,2) = {' '} ; % in case numerics follow each other
A = strread([s2{:}],'%f') ;

Of course, you have to sort out which values in A represent what
property ...

hth
Jos
spok
2006-06-05 14:39:41 UTC
Permalink
hum, I did not think someone would come back on this post !! :o)

anyway, interesting stuff you propose,
true that you need to know whichnber correspond to which, but it's
rather nice way to do it !

I'm not working on that anymore, so that I wont bother changing my code
anymore,
but still, I keep that in my pocket !! ;o)
(I did not know about the regexprep function)

cheers,

S.

Loading...