MATLAB Answers

0

preallocating a structure with fieldnames in the form of a string from another structure

David Braden さんによって質問されました 2018 年 7 月 17 日
最新アクティビティ David Braden さんによって コメントされました 2018 年 7 月 18 日
say i have a nested structure called tire
tire =
struct with fields:
lat: [1×1 struct]
cmb: [1×1 struct]
tire.lat=
struct with fields:
p8: [1×1 struct]
p10: [1×1 struct]
p12: [1×1 struct]
p14: [1×1 struct]
p12i: [1×1 struct]
I had previously manually entered:
fy_offsets=struct('p8',[],'p10',[],'p12',[],'p14',[],'p12i',[])
fy_offsets =
struct with fields:
p8: []
p10: []
p12: []
p14: []
p12i: []
In order to create an empty struct to fill with data from later processing but want it automated and self forming. I used the following code:
ps=fieldnames(tire.lat); %which pressures do we have in fields tire.lat
p=ps'; %designated pressures in cell vector for new struct fieldnames
p_str=strjoin(ps,"',[],'") %adds ,[], between every entry in string
str_begin=("'") %places ' at the leading position
str_end=("',[]") %places ,[] at the end of the string
p_input=strcat(str_begin,p_str,str_end) %concatnates the strings into one string with one entry "'p8',[],'p10',[],'p12',[],'p14',[],'p12i',[]"
fy_offsets=struct(p_input) %tries to create struct with empty fields for filling
Outputs are as follows:
ps =
5×1 cell array
{'p8' }
{'p10' }
{'p12' }
{'p14' }
{'p12i'}
p =
1×5 cell array
{'p8'} {'p10'} {'p12'} {'p14'} {'p12i'}
p_input =
"'p8',[],'p10',[],'p12',[],'p14',[],'p12i',[]"
fy_offsets=struct(p_input)
Error using struct
Conversion to struct from string is not possible.
Is there a way to get the struct function to accept this input?

  2 件のコメント

David Braden 2018 年 7 月 17 日
Turn out my other code doesn't actually need it, but i'm still curious.
dpb
2018 年 7 月 17 日
Not completely directly that I see, no. You can use dynamic field names as character string, but the value entry has to be numeric rather than string. That is,
>> fn
fn =
5×1 cell array
{'p8' }
{'p10' }
{'p12' }
{'p14' }
{'p12i'}
>> struct(fn{1},[]) % works
ans =
struct with fields:
p8: []
but when try to incorporate the brackets as text as in
>> instr=[fn{1} ',[]'] % doesn't
instr =
'p8,[]'
>> struct(instr)
Error using struct
Conversion to struct from char is not possible.
>>
You could make it work by resorting to eval, but that's difficult to code and even more difficult to debug.

サインイン to comment.

2 件の回答

回答者: Philip Borghesani 2018 年 7 月 17 日
編集済み: Philip Borghesani 2018 年 7 月 17 日
 採用された回答

Don't use strings and don't think about eval. This could be done with colon expansion and a cell array to produce a comma separated list for the call to struct (exercise for reader or I can follow up later if your stuck) or with cell2struct:
ps=fieldnames(tire.lat);
newstruct=cell2struct(cell(numel(ps),1),ps);

  2 件のコメント

dpb
2018 年 7 月 18 日
Good on you, Philip...wasn't thinking of all possibilities...
David Braden 2018 年 7 月 18 日
That seems to work nicely and should expand with the number of fields. Thank you.

サインイン to comment.


回答者: Bruno Luong
2018 年 7 月 18 日

stemplate=struct('p8',1,'p10',2,'p12',3,'p14',4,'p12i',5)
structarg = fieldnames(stemplate)';
structarg(2,:)={[]};
sempty=struct(structarg{:})
structarg(2,:)={cell(1,5)};
semptyarray=struct(structarg{:})

  0 件のコメント

サインイン to comment.



Translated by