What's wrong with textscan syntax?

5 vues (au cours des 30 derniers jours)
Sonya Burrill
Sonya Burrill le 26 Avr 2019
Modifié(e) : Stephen23 le 6 Sep 2023
I am converting some OCTAVE code to MATLAB and am running into what I assume is a sytnax error with the textscan function:
fid = fopen(FileName);
RawData = textscan(fid,'%q,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f'...
'%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f','Headerline',1,'Delimiter',',');
fclose(fid);
ERROR:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
I have tried adding a comma before the continued string on the third line:
,'%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f','Headerline',1,'Delimiter',',');
ERROR:
Error using textscan
Name-value pair arguments must come in pairs.
Which to me means the comma should not be there, as it is picking that up as the start of a name-value pair.
I have looked up examples, but cannot find why I am getting an error.

Réponse acceptée

Star Strider
Star Strider le 26 Avr 2019
Try an alternate format descriptor:
fd = ['%q' repmat('%f', 1, 28)];
RawData = textscan(fid, fd, 'HeaderLines',1, 'Delimiter',',');
I believe I counted correctly. It would be best to check that.
  3 commentaires
Star Strider
Star Strider le 26 Avr 2019
As always, my pleasure.
Stephen23
Stephen23 le 6 Sep 2023
Modifié(e) : Stephen23 le 6 Sep 2023
"Also the main problem was that I had used the name HeaderLine rather than HeaderLines."
Actually the cause of the error you show in your question is a missing comma.
Just as the error message states, this missing comma is invalid syntax:
RawData = textscan(fid,'%q,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f'...
% you missed a comma here ^^
After fixing that you would then find that it is not valid to supply the format string as two separate input arguments, i.e. character vectors over multiple lines need to be explicitly concatenated together using square brackets:
RawData = textscan(fid,['%q,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f',...
'%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f'],'Headerline',1,'Delimiter',',');
That is what Star Strider avoided by using REPMAT and supplying the format text as one argument.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by