在MATLAB中使用xlsread读取数字和字符串数据

时间:2012-04-14 19:59:49

标签: excel matlab file-io

我在Excel中有一大列数据,我想读入一个字符串单元格数组。但是,有些条目是数字,其余的是字符串。所以我有类似

的东西
288537
288537
312857
589889
589889
1019503
1019503
1098802
1098802
abc
efg
hij
1992724

第一行是标题行,所以我们忽略它。当我使用

[~, ID] = xlsread('data.xlsx', 'A2:A125581')

ID仅包含字符串条目,而不包含数字条目。

如何让xlsread将数字视为字符串,以便我可以将所有内容都读成字符串?

1 个答案:

答案 0 :(得分:4)

XLSREAD返回三个输出。第三个是包含已读取的所有内容的单元数组。但是,单元格数组的数字是数字的数字,所以如果你想把所有东西都作为字符串,你必须转换它们:

%# read everything into one cell array
[~,~,raw] = xlsread('data.xlsx', 'A2:A125581');
%# find numbers
containsNumbers = cellfun(@isnumeric,raw);
%# convert to string
raw(containsNumbers) = cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);