在MATLAB中使用xlsread来读取数字和字符串数据 [英] Using xlsread in MATLAB to read number and string data
问题描述
288537
288537
312857
589889
589889
1019503
1019503
1098802
1098802
abc
efg
hij
1992724
第一行是标题行,所以我们忽略它。当我使用
[〜,ID] = xlsread('data.xlsx','A2:A125581')
ID
只包含字符串条目,而不是数字条目。
如何获取 xlsread
将数字视为字符串,因此我可以将所有内容作为字符串读取? / p>
XLSREAD 返回三个输出。第三个是包含已读取的所有内容的单元格数组。但是,单元格数组的数字是数字的数字,所以如果你想要所有的字符串,你必须转换这些:
%#将所有内容读入一个单元格数组
[〜,〜,raw] = xlsread('data.xlsx','A2:A125581');
%#find numbers
containsNumbers = cellfun(@ isnumeric,raw);
%#转换为字符串
raw(containsNumbers)= cellfun(@ num2str,raw(containsNumbers),'UniformOutput',false);
I have a large column of data in Excel that I'd like to read into a string cell array. However, some of the entries are numbers, and the rest are strings. So I have something like
288537
288537
312857
589889
589889
1019503
1019503
1098802
1098802
abc
efg
hij
1992724
The first row is a header row, so we ignore that. When I use
[~, ID] = xlsread('data.xlsx', 'A2:A125581')
ID
contains only the string entries, not the numeric entries.
How can I get xlsread
to treat the numbers as strings, so I can read everything as a string?
XLSREAD returns three outputs. The third one is a cell array containing everything that has been read. However, the cell array has numbers where the data is numeric, so if you want everything as strings, you have to convert these:
%# 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);
这篇关于在MATLAB中使用xlsread来读取数字和字符串数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!