bnbfreak
bnbfreak

Reputation: 353

Scan multi rows using textscan in matlab

I have variable A which contains 2 rows string with the same number of columns as follows :

 0 -> 2 1.000000 1.000200 A-MPDU 1.000000 1.000100 SUCCESS 1.000100 1.000200 FAIL NO
 0 NO NaN 1.000270 1.000570 BACKOFF NaN NaN NO NaN NaN NO NO

But when I try to use this command :

C = textscan(input_str,'%d %s %d %f %f %s %f %f %s %f %f %s %s');

It said "Error using textscan. First input must be of type double or string". When the data contains only one row, there is no error. How to read those 2 lines correctly ?

Upvotes: 1

Views: 104

Answers (1)

Dev-iL
Dev-iL

Reputation: 24159

I'm unsure what caused your problem - your code work correctly on my MATLAB (on version 2012b). One reason I could think of, is that your were testing textscan with a string rather than a file, and you didn't add char(10) at the end of the first line:

input_str = [...
'0 -> 2   1.000000 1.000200 A-MPDU  1.000000 1.000100 SUCCESS 1.000100 1.000200 FAIL NO' char(10)...
'0 NO NaN 1.000270 1.000570 BACKOFF NaN      NaN      NO      NaN      NaN      NO   NO'];

(where char(10) is a newline character, "\n", just like inside an actual file)

textscan also works if input_str points to a file, i.e. input_str = fopen('t.txt');.

Another possible explanation is if A is a cell array, in which case textscan doesn't know what to do with it. To overcome this you can iterate over the cells and send textscan one string at a time.

Upvotes: 0

Related Questions