Matlab - 读取特定的格式行

时间:2013-04-26 08:58:20

标签: matlab csv file-io double-quotes

我有一个文件,其中包含以下格式0,"20 300 40 12"的数据。

如何使用sscanf函数读取此数据,以便将0存储在单独的变量中,将20 300 40 12存储在另一个变量中。问题是“”中的数组改变了它的大小,所以我不能使用修复长度数组。所以我可以在我的文件中包含这样的内容:

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

你能给我一些如何阅读的提示吗?

2 个答案:

答案 0 :(得分:2)

你试过这个:

fid = fopen(filename,'r');  
A = textscan(fid,'%d,%q','Delimiter','\n');

答案 1 :(得分: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};