# How does one save a struct type to an individual cell inside a table?

25 views (last 30 days)
Rodrigo Ceballos on 31 Oct 2016
Commented: Peter Perkins on 2 Nov 2016
I'm trying to make a function with the following signature:
function [test_result,model] = test_and_save(trainer,trainer_params,test,test_params,notes)
Which is a wrapper to training an arbitrary machine learning algorithm and using an arbitrary test on it, saving all the information about this operation to a table. The trainer_params and test_params are structures of arbitrary size and shape. So far, all my efforts fail in the process of trying to convert these arrays into something the table will accept because its checking for consistency among the different structures in the different rows. How do I avoid this?
Thanks!
Rodrigo
##### 2 CommentsShowHide 1 older comment
Peter Perkins on 2 Nov 2016
Following up on Walters comments, if the structures are all different, including their fields, the only way you're going to be able to store them in a table is inside a cell array, one struct array per cell. For example
>> s1 = struct('a',1,'b',2);
>> s2 = struct('c',3,'d',4);
>> t = table([1;2],{s1;s2})
t =
2×2 table array
Var1 Var2
____ ____________
1 [1×1 struct]
2 [1×1 struct]
>> t.Var2
ans =
2×1 cell array
[1×1 struct]
[1×1 struct]
As opposed to
>> s1 = struct('a',1,'b',2);
>> s2 = struct('a',3,'b',4);
>> t = table([1;2],[s1;s2])
t =
2×2 table array
Var1 Var2
____ ____________
1 [1x1 struct]
2 [1x1 struct]
>> t.Var2
ans =
2×1 struct array with fields:
a
b
The table display looks the same in both cases, but that's just a display limitation. In the first case, as can be seen, Var2 is a 2x1 cell array, each cell containing a scalar struct. In the second case, Var2 is a 2x1 struct.
It's hard to tell from what you've said what you have or what you want.

Marc Jakobi on 31 Oct 2016
Have you tried converting the struct to a cell?
doc struct2cell