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?

 采纳的回答

>> 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 个评论

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',',');

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by