I want to put together 2 cells of different sizes in the same struct
. For example:
a = {'one', 'two', 'three'};
b = {'four', 'five', 'six', 'seven'};
struct("setA", a, "setB", b);
Whenever I try to do this, MATLAB throws the following error:
error: struct: dimensions of parameter 2 do not match those of parameter 4
According to the error message the problem is the dimensions of the cells. Furthermore, if I remove one element from cell b
the process finishes without errors:
a = {'one', 'two', 'three'};
b = {'four', 'five', 'six'};
struct("setA", a, "setB", b);
Any suggestions?
You need to wrap the cells in another cell to create a scalar struct
containing cells in it's fields.
struct('setA', {a}, 'setB', {b})
% setA: {'one' 'two' 'three'}
% setB: {'four' 'five' 'six' 'seven'}
By default, struct
assumes that a cell means that you want a multi-element struct
where each cell element will belong to a different struct
. It uses the dimensions of these cells to determine the size of the resulting struct
. In your case, the two cell arrays (a
and b
) are different sizes so it gets confused.
By wrapping each of them inside of another cell, MATLAB will create a scalar struct containing your cell arrays as you expect.