List of structures as input to getfield()?

The getfield() is defined as:
value = getfield(myStruct, 'myField').
Now I have a list of structures, myStructList, and I'd like getfield() to run through the filenames of that list like:
value(i) = getfield(myStructList(i).name,'myField')
But this gives an error message, any idea how this can be done?
Error messages are:
Attempt to reference field of non-structure array.
Error in getfield (line 36)
f = s.(deblank(strField)); % deblank field name

댓글 수: 3

Star Strider
Star Strider 2015년 5월 16일
What error message does it give?
Please copy all the red text from the Command Window and Edit your Question to include it.
uffeaf
uffeaf 2015년 5월 16일
Thanks, done.
Image Analyst
Image Analyst 2015년 5월 16일
How did you get myStructList? Did you get it from a call to dir()?

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

답변 (2개)

Nobel Mondal
Nobel Mondal 2015년 5월 16일

0 개 추천

First Argument of 'getfield' should be a struct. `myStructList(i).name` probably refers to a char type variable. If myStructList is essentially an array of structures, you could try
>> getfield(myStructList(i),'myField')
However, as Star Strider said, we could be more useful after looking at the error stack.
Walter Roberson
Walter Roberson 2015년 5월 16일

0 개 추천

In your code,
myStructList(i).name
would be a file name. And you are then trying to get the MyField field of the file name.
Is myStructList(i).name intended to be a string that is a variable name and you want to look up that variable and get the MyField field of that variable? If so, don't do that.
If you absolutely must work with strings that name variables, then put all of the variables as fieldnames into a single structure. For example
MyStruct.vodoo23
MyStruct.skidoo
instead of two variables, one named vodoo23 and the other named skidoo.
If you put them all in one structure, then you can use dynamic field names.
value(i) = MyStruct.(myStructList(i).name).myField);

카테고리

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

질문:

2015년 5월 16일

편집:

2015년 5월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by