convert number from cell to double, cell2mat not working

时间:2015-09-14 15:49:59

标签: matlab

I think I'm losing my mind.

I have a 3 x 2 cell which looks exactly like below.

 Region  Code
 US      1
 EU      2

I then have the following code to determine the row number for the EU region.

eq_code_index = find(ismember(fund.type_des(:, 1), 'EU'));
eq_code = cell2mat(fund.type_des(eq_code_index, 2));

eq_code_index returns 3 which is correct (row headers are included in the output). So I want the value in row 3, column 2 which is 2. I then use cell2mat to convert it from a cell value to an integer however it doesn't work the value is of type char? Haven't a clue why cell2mat isn't working?

Update

Even if I do the following two lines of code below I can't get the codes into a vector, they turn into char's

codes = fund.type_des(2:end, end);
codes = cell2mat(codes)

1 个答案:

答案 0 :(得分:3)

To access a single element in a cell array, use curly braces:

fund.type_des{eq_code_index, 2};

This is generally simpler than using cell2mat(). If the contents of the cell are chars and you want an integer, you have to perform the conversion. str2num() is one of many options for this:

eq_code = str2num(fund.type_des{eq_code_index, 2});