I have a structure within a structure (a nested structure), and am trying to add a field to that structure, what should I write?
3 ビュー (過去 30 日間)
古いコメントを表示
s.Field.(FieldName)=zeros(1,1);
Doesn't work...
0 件のコメント
回答 (1 件)
Fabian Schadt
2017 年 3 月 23 日
編集済み: Stephen23
2017 年 3 月 23 日
Hi Guy Nir,
actually there should be no problem.
s.Field.FieldName = zeros(1,1);
works ;-)
However, if you state your nested struct with brackets ->
s.Field.(FieldName),
then you access with (FieldName) a variable in your workspace. If this does not exist, you receive an error.
If brackets, then it would be like this:
FieldName = [];
s.Field.(FieldName) = zeros(1,1);
Hope I could help!
5 件のコメント
James Tursa
2017 年 3 月 23 日
編集済み: James Tursa
2017 年 3 月 23 日
Works for me:
>> clear all
>> FieldName='valid'
FieldName =
valid
>> s.Field.(FieldName) = zeros(1,3)
s =
Field: [1x1 struct]
>> s.Field.valid
ans =
0 0 0
You should double check what the variable s really is. Are you sure it isn't maybe a cell array that contains structures, or something like that?
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!