在matlab中用逗号作为小数点读取分号分隔数据的CSV

Read CSV with semicolon separated data with comma as decimal mark in matlab

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

1,000333e+003;6,620171e+001
1,001297e+003;6,519699e+001
1,002261e+003;6,444984e+001

想把数据读入matlab,但是csvread要求用逗号分隔,一直没找到逗号-小数点的解决方法。我想我可以以某种方式使用 textscan?

很抱歉提出这样一个(我认为)简单的问题,但我希望有人能提供帮助。 None of the other questions/answers in here seems to dealing this combination of comma and semicolon.

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

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

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

M = dlmread(filename,';')

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

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

%rt is permission r for read t for open in text mode
csv_file = fopen('D:\Dev\MATLAB\Whosebug_tests.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);

结果显示。

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

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)

结果:

converted_data{1} =
   1.0e+03 *
    1.0003
    1.0013
    1.0023
converted_data{2} =
   66.2017
   65.1970
   64.4498
% Data is in C:\temp\myfile.csv

fid = fopen('C:\temp\myfile.csv');
data = textscan(fid, '%s%s', 'delimiter', ';');
fclose(fid);

% Convert ',' to '.'
data = cellfun( @(x) str2double(strrep(x, ',', '.')), data, 'uniformoutput', false);


data = 

    [3x1 double]    [3x1 double]

data{1}

ans =

   1.0e+03 *

   1.000333000000000
   1.001297000000000
   1.002261000000000


data{2}

ans =

  66.201710000000006
  65.196990000000000
  64.449839999999995