Matlab - 使用复杂的字符串读取csv文件

时间:2013-10-22 18:43:23

标签: matlab csv

我有一个csv文件,其中包含以下行:

"some text", "more text hello 392 392", "etc complicated string here with spaces and commas"

如何以大矩阵形式阅读此文件?

2 个答案:

答案 0 :(得分:0)

因此,您要求简单的文本阅读?

fid=fopen('dummy.csv');
a=[];
while(1) ;
    tline = fgetl(fid);
    if ~ischar(tline)
        break;
    end
    a=[a;char(tline)];
end
fclose(fid);

答案 1 :(得分:0)

在我看来,最明智的方法是使用正则表达式来匹配正确的模式(引号和逗号对):

%// Read lines as strings
fid = fopen('input.txt', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);

%// Tokenize each line using a regular expression
C = cellfun(@(x){[x{:}]}, regexp(C{:}, '"([^"]*)"(?:\s*,\s*)?', 'tokens'));

生成的单元格数组C应包含所有所需的逗号分隔值作为标记。

实施例

假设您的文件名为“input.csv”,并包含以下内容:

"some text", "more text hello 392 392", "string spaces and commas ,,,"
"hello, world!"

后:

fid = fopen('input.csv', 'r');
C = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
C = cellfun(@(x){[x{:}]}, regexp(C{:}, '"([^"]*)"(?:\s*,\s*)?', 'tokens'));

结果应该是:

C(1, 1) =
    'some text'
    'more text hello 392 392'
    'string spaces and commas ,,,'

C(1, 2) = 
    'hello, world!'