Matlab extract numbers from cell with mixed contents

0

I have a cell array with different datatypes, from an excel sheet that I need to read in, but which is beyond my control.

[~, ~, raw] = xlsread(xlsFile)

raw is now a r-by-c cell vector, for simplicity assume it has only one row. It will be filled with numbers (excel datenums to be exact) or other values such as [NaN] or ''. Thus, cell2mat() will not work.

What is the best way to extract only the number values? Is there a way to get an index where a cell contains a number?

Thanks.

zuiqo

Posted 2014-12-08T16:11:33.707

Reputation: 676

Answers

0

Assuming just a single row, the following works to eliminate non-numeric values

rawData = {5,1.77,NaN,''};
rawDataIsNum = cell2mat(cellfun(@isnumeric,rawData,'UniformOutput',false)); %Returns matrix of true/false if real number or NaN
data = rawData(rawDataIsNum); %Returns cell of numbers

To remove the NaNs you can use something similar with @isnan

jebob

Posted 2014-12-08T16:11:33.707

Reputation: 1