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

조회 수: 15 (최근 30일)
Rodrigo Ceballos
Rodrigo Ceballos 2016년 10월 31일
댓글: Peter Perkins 2016년 11월 2일
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
Walter Roberson
Walter Roberson 2016년 10월 31일
If I understand correctly, you have a collection of structs of arbitrary size and shape to store into a table, and the bit about machine learning is not relevant to the matter at hand (it is where the data came from, but otherwise not important.)
I think what we need to know now is (A) whether the different structs have the same field names; (B) if the structs do have the same field names, then do the have consistent size and shape of content for each field with the same name; and (C) how do you want all of this to be imported into the table?
For example if the first struct is 1 x 11, and the second struct is 3 x 8, then are the two to become just two different entries into the same variable in the table(); or do the field names correspond to the table variable names and the first struct should become a series of 1 x 11 entries in a row in the table, and the second struct should become a series of 3 x 8 entries in another row of the table; or do the field names correspond to the table variable names and the first struct should expand to 11 rows in the table and the second struct should expand to 24 rows in the table, or ...?
Peter Perkins
Peter Perkins 2016년 11월 2일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Marc Jakobi
Marc Jakobi 2016년 10월 31일
Have you tried converting the struct to a cell?
doc struct2cell

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by