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

조회 수: 26(최근 30일)
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표시숨기기 이전 댓글 수: 1
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 2016년 10월 31일
Have you tried converting the struct to a cell?
doc struct2cell

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

### 범주

Find more on Structures in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by