Using xlsread in MATLAB to read number and string data

excel, file-io, matlab

Solution

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);

Problem

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?

Original source