Hi,
I have structure, for example
S.Level0=0
I have a field structure I want to add to S namely
S.Level1.SubLevel0.SubLevel1.SubLevel2.Text = '1234';
where i have the above fields defined in a cell array
f = {'SubLevel0','SubLevel1','SubLevel2','Text'}
and the string cvar='1234' being the required value
is there an elegant way to produce
S.Level1.SubLevel0.SubLevel1.SubLevel2.Text='1234'
using f & cvar ?
Thank You

 채택된 답변

Paul
Paul 2022년 6월 11일

3 개 추천

S.Level0=0;
f = {'SubLevel0','SubLevel1','SubLevel2','Text'};
cvar = '1234';
S = setfield(S,'Level1',f{:},cvar);
S.Level0
ans = 0
S.Level1.SubLevel0.SubLevel1.SubLevel2.Text
ans = '1234'

댓글 수: 4

Yep, still a bad idea. How are you going to refer to those fields later in your code if you don't know their names? If you knew their names, then you'd just use them. But you don't and making up names according to some strings in a cell array. Let's say you want to put the text in a plot title or something
title(S.Level1.SubLevel0.SubLevel1.SubLevel2.Text);
OK but if the string in "f" changed, then that won't work. It's not robust or general purpose.
Again, you can do this, with parentheses and dynamic field names, but it's universally frowned upon and I doubt you will see any expert MATLAB programmer do that.
I don't know where the OP is going with this and so don't have a position on whether or not it's the right way to go. Just trying to answer the question. Having said that, getfield() can be used in a similar fashion to extract based on f.
S.Level0=0;
f = {'SubLevel0','SubLevel1','SubLevel2','Text'};
cvar = '1234';
S = setfield(S,'Level1',f{:},cvar);
plot(rand(3))
title(getfield(S.Level1,f{:}))
Paul Mitchell
Paul Mitchell 2022년 6월 12일
Thank you very much for spending time to provide me with the solution. I thought I had tried everything possible !! Clearly I did not - Regards Paul

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

추가 답변 (2개)

Image Analyst
Image Analyst 2022년 6월 11일

0 개 추천

Having field names that are unknown in advance, and their names are only determined by getting their name from a cell array variable sounds like a bad idea. It's probably one of the worst and most common ideas and it comes up every week.
However it is possible to do bad things in MATLAB and the FAQ will show you how to do it and explain why it's bad.
I'm sure Stephen will also chime in with additional reasons why you really don't want to do this even though you think you do.

댓글 수: 2

Paul Mitchell
Paul Mitchell 2022년 6월 12일
I know its bad, but the field names are unknown in advance (and could change - hence running the conversion process multiple times)
Image Analyst
Image Analyst 2022년 6월 12일
The solution you accepted will do what you want but as you can see, to get this unknown field name you have to do some pretty cryptic stuff that makes the code almost unmaintainable. Would be better to use fixed and known names rather than dynamically varying names, as I hope someday you realize. But good luck with it. You might also like to read the FAQ:
And Stephen's tutorial:

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

Dror Liran
Dror Liran 2023년 12월 20일
편집: Dror Liran 2023년 12월 24일

0 개 추천

What you are after is called dynamic field names, the matlab documatation is here: https://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html
Anyway the way to do it is something like:
names = ["name1" "name2"];
a.fld1 = 1;
a.fld2 = "a";
a.(names(1)) = "whatever";

댓글 수: 3

Yes, but you'll forever be referring to a.((names(1)). You can never refer to a.name1 because that field name is in a variable that presumably might change from run to run. If you did, then that means the field name never varies, and if it never varies, why not just simply use a fixed field name and not worry about the parentheses? Like
a.var = "whatever";
Dror Liran
Dror Liran 2023년 12월 24일
I have tried it and it works for me on matlab 2023b
Stephen23
Stephen23 2023년 12월 24일
"I have tried it and it works for me on matlab 2023b"
It should work in R2023b, dynamic fieldnames were added more than twenty years ago in R13:

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

카테고리

도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2022년 6월 11일

댓글:

2023년 12월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by