Search code examples
matlabdoublecellint32

How can I change a cell array consisting of int 32 and double to matrix of doubles?


I have a structure that I want to change it to a matrix. So I got cell2mat(struct2cell(d)). But struct2cell(d) gives me

6×1 cell array

{1100×1 int32 }
{1100×1 int32 }
{1100×1 int32 }
{1100×1 int32 }
{1100×1 double}
{1100×1 double}

and cell2mat(struct2cell(d)) gives me the error:

All contents of the input cell array must be of the same data type.

So my question is how can I convert all of them to double? Or how can I get a matrix finally?


Solution

  • You can cast each elements of your cell with cellfun (which is basically a hidden for loop):

    %Dummy data
    s.a = int16([1:4])
    s.b = linspace(0,1,4)
    
    %struct -> mat
    res = struct2cell(s);
    res = cellfun(@double,res,'UniformOutput',0) %cast data type to double
    res = cell2mat(res)