How can i seperate numbers from strings while using textscan?

I want to store this data in a cell using textscan:
2000,146.557,OE,-300,150
2000,146.625,FOB,0,0
My code is
fidT=fopen('test.txt');
dataT=textscan(fidT,'%d,%f,%s,%d,%d');
But matlab handles "OE,-300,150" as one string. I want matlab to handle it as 1 string, followed by 2 numbers. How can I do that?

 Risposta accettata

Stephen23
Stephen23 il 19 Gen 2017
Modificato: Stephen23 il 19 Gen 2017
>> str = '2000,146.557,OE,-300,150 2000,146.625,FOB,0,0';
>> textscan(str,'%f%f%s%f%s%f%s%f%f','Delimiter',',')
ans =
Columns 1 through 7
[2000] [146.557] {1x1 cell} [-300] {1x1 cell} [146.625] {1x1 cell}
Columns 8 through 9
[0] [0]
If you edit your question and upload the data file using the paperclip button then I can also show you how to make it work for your file.
Note that the 150 2000 is parsed as one string, because you did not put a comma. Is this a mistake?

1 Commento

Thanks for reply. I already solved it by using this line.
fid=fopen('test.txt');
data=textscan(fid,'%d%f%s%d%d%d%s%s','Delimiter',',');

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by