Search code examples
matlabcell-array

"Conversion to double from cell is not possible." but input is cell array of doubles


I have the following code:

Xh = zeros(InfDS.statedim,N);                          
Xh(:,1) = str2double(states_0(2:end,2));
Px = diag(str2double(states_0(2:end,3)));

Where states_0 is a Nx3 cell array. It could for instance look as follows:

''       'x0'       'P0'  
'SOC'    '100'      '0.75'
'RI'     '0.001'    '0.75'

but it could also contain more parameters.

My issue is that I have rewritten part of the code, s.t. in no longer return a cell array containing strings, but a cell array containing doubles.

[       100]    [0.7500]
[1.0000e-03]    [0.7500]

I figured this would be easier to work with due to the fewer conversions, but it seems not to be... I have tried:

Xh = zeros(InfDS.statedim,N);
Xh(:,1) = states_0(1:end,1);
Px = diag(states_0(1:end,2));

but that gives me the error:

Conversion to double from cell is not possible.

Error in testfile (line 61)
Xh(:,1) = states_0(1:end,1);

Could someone explain why this is the case? I figured this would be a better way of doing things, but it seems outright illegal...


Solution

  • It seems like cell2mat() does the trick!

    Note: I guess string2double implicitly converts to an array?