4

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?

2

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.