1

I have a file which contains data in the following format 0,"20 300 40 12".

How can I read this data with sscanf function such that I store 0 in a separate variable and 20 300 40 12 in another variable. The problem is that the array within the " " changes its size, so I cannot use a fix length array. So I can have something like this within my file:

0,"20 300 40 12"
0,"20 300 43 40 12"
1,"22 40 12"

Can you give me a hint of how to read this?

4

2 回答 2

2

你试过这个:

fid = fopen(filename,'r');  
A = textscan(fid,'%d,%q','Delimiter','\n');
于 2013-04-26T09:10:07.540 回答
0

这是另一种方法:

[a,b] = textread('ah.txt','%d,"%[^"]"');
fun = @(x) split(' ',x);
resb = cellfun(fun,b,'UniformOutput',false)
res = {a resb};

function l = split(d,s)
%split string s on string d
out = textscan(s,'%s','delimiter',d,'multipleDelimsAsOne',1);
l = out{1};
于 2013-04-26T09:23:51.000 回答