使用分号分隔数据读取CSV,并在matlab中使用逗号作为十进制标记

Nie*_*ler 4 csv matlab textscan

我的问题是,我有以下格式的CSV数据:

1,000333e+003;6,620171e+001
1,001297e+003;6,519699e+001
1,002261e+003;6,444984e+001
Run Code Online (Sandbox Code Playgroud)

我想将数据读入matlab,但csvread要求它以逗号分隔,并且我无法找到逗号小数标记的解决方案.我想我可以用textscan某种方式?

我很遗憾地问这个(我认为)这个简单的问题,但我希望有人可以提供帮助.这里的其他问题/答案似乎都没有处理逗号和分号的这种组合.

bil*_*aly 9

EDIT3(接受的答案):使用主工具栏的变量部分中的导入数据按钮,可以自定义数据的导入方式.完成后,您可以单击箭头下方的导入选择,并生成将遵循导入数据窗口中定义的相同规则的脚本或函数.

导入数据说明

-------------------------------------------------- 留作参考 ----------------------------------------------- ---

你可以使用dlmread它以下列格式工作

M = dlmread(filename,';')

filename是一个包含文件完整路径的字符串,除非该文件位于当前工作目录中,在这种情况下您只需键入文件名即可.

EDIT1:改为使用textscan,下面的代码应该可以解决问题,或者至少应该使用大部分代码.

%rt is permission r for read t for open in text mode
csv_file = fopen('D:\Dev\MATLAB\stackoverflow_tests\1.csv','rt');

%the formatspec represents what the scan is 'looking'for. 
formatSpec = '%s%s';

%textscan inputs work in pairs so your scanning the file using the format
%defined above and with a semicolon delimeter
C = textscan(csv_file, formatSpec, 'Delimiter', ';');

fclose(csv_file);
Run Code Online (Sandbox Code Playgroud)

结果显示出来.

C{1}{1} =
1,000333e+003
C{1}{2} =
1,001297e+003
C{1}{3} =
1,002261e+003
C{2}{1} =
6,620171e+001
C{2}{2} =
6,519699e+001
C{2}{3} =
6,444984e+001
Run Code Online (Sandbox Code Playgroud)

EDIT2:用点替换逗号并转换为double类型的整数:

[row, col] = size(C);
for kk = 1 : col
    A = C{1,kk};
    converted_data{1,kk} = str2double(strrep(A, ',', '.'));
end

celldisp(converted_data)
Run Code Online (Sandbox Code Playgroud)

结果:

converted_data{1} =
   1.0e+03 *
    1.0003
    1.0013
    1.0023
converted_data{2} =
   66.2017
   65.1970
   64.4498
Run Code Online (Sandbox Code Playgroud)