MATLAB: In a struct, how can you extract values for a vector of fieldnames

getfieldstruct

Hello,
I have a struct called MyStruct. I can extract the field names with the command
MyFieldNames = fieldnames(MyStruct);
If I'm interested in extracting the values for first n of MyFieldNames, is there a way I can do this using the "getfield" command? If I try
Temp = getfield(StateSpace, MyFieldNames(1:n))
or
Temp = Temp = getfield(StateSpace, MyFieldNames)
I get an error saying, "Index exceeds matrix dimensions."
Thank you, Kevin

Best Answer

MyFieldNames = fieldnames(MyStruct);
for i=1:3
MyValues(i,1) = getfield(MyStruct,MyFieldNames{i});
end