Hello,
I have a vector (1D array) of a class that has several fields.
When working with my class, I would like to sometimes obtain a simple vector of a specific field, but I have only been able to do that with a for loop. There must be a better way!
For example, I have a class to represent a person called meep. A field within meep is age.
If I have vector containing 1000 meep, I might like to extract a numeric vector containing just the ages.
I thought something like
might do the trick, but it just gives the age of the last element of the meep vector. I've resorted to this static helper function within my class:
function param=paramVec( mVec, field )
n=length(mVec);
param=-99*ones(n,1);
for i=1:n
param(i)=mVec(i).(field);
end
end
Can someone suggest a more sensible and faster running approach?
Thank you, David
댓글 수: 0
댓글을 달려면 로그인하십시오.