Search code examples
matlabmatlab-class

MATLAB: How do I get the value of a class property given a property name


If I have a class defined as

classdef myclass
  properties
     foo = 3;
     bar = 7;
  end
end

And I want to access property foo I would write

obj = myclass()
obj.foo % Gives me 3

But, if I only have a string representation of the property name, and don't know which property it is how would I do it then? As in the example below:

obj.someFunction('foo')  % or
someFunction(obj, 'foo') % should both give me the value of obj.foo

What I want to do is have a list of properties, iterate through it and get the value for a specific object. It seems like it should be possible, but I failed to find it in the documentation.


Solution

  • value = getfield(struct, 'field')