convert cell array of different data type char and number to matrix

I have a xls sheet and i read it using xlsread() function , and its produce a cell array of character and number. the first column ( ID: Char&number) and the rest is number , i used cell2mat() , but it does not work it produce error data should be same type. then i separate the ID from the whole cell it produce another cell ( ID ) then when i tried convert it to mat , iam receiving error again ? please any suggestion.

Answers (2)

You have several options.
1) If every cell in your cell array contains a scalar double value, cell2mat() will work properly. You probably still have other data types mixed into your cell array.
2) You can get a numeric array directly from xlsread by using xlsread with this syntax: num = xlsread('filename.xls');
3) Instead of xlsread, try using readtable('filename.xls'). This will return a table instead of a cell array or double array.
[num, txt, raw] = xlsread('YourFile.xls');
then num should already be the numeric data without the first (text) column.
If you have a particular reason for creating a subset:
raw_without_1 = raw(:,2:end);
[nn_r, nn_c] = find(~cellfun(@isnumeric, raw_without_1));
if ~isempty(nn_r)
content = raw_without_1{nn_r(1),nn_c(1)};
fprintf('Some cells are not numbers, such as location (%d,%d) which has class "%s" and value\n', nn_r(1), nn_c(1), class(content));
disp(content);
num = [];
else
num = cell2mat(raw_without_1);
end

7 Comments

@walter,kirby :my issue is the (ID which is "char&Num" , txt ) the first column you exclude and its a cell.How can i convert it to array its one cell cell2mat() does not work on it ? bellow is an example of my data type:
ID A B
A_P66027 6.499845887 6.488844346
A_P77178 5.920352855 6.47370575
A_P212522 8.491352074 8.540006921
A_P934473 5.815383296 5.917372079
A_P9671 11.52092555 11.6364547
I should convert the ID too , to array.
You cannot convert alphanumeric strings into numeric arrays.
It could be that you do not want to convert the entire string, just the numeric portion of it. If so then we need more information about what form the IDs can take.
If they all match the format A_P followed by digits, then one code to convert would be
ID_num = cellfun( @(S) str2double(S(4:end)), raw(:,1));
the result would be a pure numeric vector.
@walter i need the character ? the code will delete A, P :(
Okay, so for A_P66027 what numeric value would you want stored?
@walter The ID should be same with out changing : because A_P66027 represent a name in another field for example : A_P66027 = walter. so i can not remove the character , just how can we convert ID cell to array.
raw(:,1) would be a cell array of strings that were just the IDs.
In MATLAB, it is not possible to have a single numeric array that also stores strings. The only kind of array (that does not use named fields) that can have both numbers and strings is a cell array, and that is what raw already is, a cell array of numbers and strings.
Perhaps you just want
IDs = raw(:,1);
numbers = cell2mat(raw(:,2:end));
after which IDs{K} would be the ID string that corresponds to numbers(K,:). If this is the case, then probably you could have gotten the same thing with
[numbers, IDs] = xlsread('YourFile.xls');
because by default xlread() divides up the numbers and text in that way.
Or perhaps you should look at the table() datatype, and the readtable() routine which can read from xlsx into tables.
Indeed readtable() seems like the best option here. The result will be a table instead of a cell array or numeric array. You can learn to interact with tables here .

Sign in to comment.

Categories

Products

Asked:

on 21 Jan 2016

Commented:

on 22 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!