Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested textscan statements

The following two statements read the first line from an input file (fid) and parse said line into strings delimited by whitespace.

a = textscan(fid,'%s',1,'Delimiter','\n');
b = textscan(a{1}{1},'%s');

I would like to know if this action can be accomplished in a single statement, having a form similar to the following (which is syntactically invalid).

b = textscan(textscan(fid,'%s',1,'Delimiter','\n'),'%s');

Thanks.

like image 649
user001 Avatar asked Dec 07 '25 12:12

user001


1 Answers

Instead of

a = textscan(fid, '%s', 1, 'Delimiter', '\n');

you can use

a = fgetl(fid);

That will return the next line in fid as a string (the newline character at the end is stripped). You can then split that line into white-space separated chunks as follows:

b = regexp(a, '\s*', 'split');

Combined:

b = regexp(fgetl(fid), '\s*', 'split');

Note that this is not 100% equivalent to your code, since using textscan adds another cell-layer (representing different lines in the file). That's not a problem, though, simply use

b = {regexp(fgetl(fid), '\s*', 'split')};

if you need that extra cell-layer.

like image 160
Florian Brucker Avatar answered Dec 09 '25 04:12

Florian Brucker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!