Matlab读取csv字符串数组

时间:2012-07-16 15:29:20

标签: arrays string matlab file-io csv

我有一个名为Book2.csv的逗号分隔数据集我想提取内容。内容是496024x1字符串数组(正常,海王星,蓝精灵)。

我试过:

 [text_data] = xlsread('Book2.csv');

但它只是输出了一个text_data空数组?

尝试csvread时

M = csvread('Book2.csv')
??? Error using ==> dlmread at 145
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==>
norma

Error in ==> csvread at 54
    m=dlmread(filename, ',', r, c);

我收到此错误。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:4)

在我的头脑中,这应该完成工作。但可能不是最好的方法。

fid = fopen(your file);  //open file
 //read all contents into data as a char array 
 //(don't forget the `'` to make it a row rather than a column).
data = fread(fid, '*char')';
fclose(fid);
//This will return a cell array with the individual
//entries for each string you have between the commas.
entries = regexp(data, ',', 'split'); 

答案 1 :(得分:0)

尝试类似:textread

data = textread('data.csv', '', 'delimiter', ',', ... 
            'emptyvalue', NaN);

答案 2 :(得分:0)

对我来说最简单的方法是:

path='C:\folder1\folder2\';
data = 'data.csv';
data = dataset('xlsfile',sprintf('%s\%s', path,data));

您也可以执行以下操作:

[data,path] = uigetfile('C:\folder1\folder2\*.csv');
data = dataset('xlsfile',sprintf('%s\%s', path,data));

现在您将数据作为数据集加载。 例如,获取第1列的简单方法是

 double(data(1))