在MATLAB中使用xlsread来读取数字和字符串数据 [英] Using xlsread in MATLAB to read number and string data

查看:5707
本文介绍了在MATLAB中使用xlsread来读取数字和字符串数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中有一大堆数据,我想读入一个字符串单元格数组。然而,一些条目是数字,其余的是字符串。所以我有一些像

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆