MATLAB Answers

juliperde

properties and object oriented programming

Asked by juliperde
on 15 Nov 2012

hi

I need to know that if there is way to call all properties in the class without calling their name. I write many function for all properties by one by. So I need one function and that contains all properties.

for example I use this:

classdef class
    properties
        x
        y
        z
    end % properties
    methods
        function obj = d(obj,x)
            obj.x(1).value(1)=x(1).value(1);
            obj.x(2).value(2)=x(2).value(2);
        end%functions
        function obj = e(obj,y)
            obj.y(1).time(1)=y(1).time(1);
            obj.y(2).time(2)=y(2).time(2);
        end%functions
        function obj = e(obj,z)
            obj.z(1).something(1)=z(1).something(1);
            obj.z(2).something(2)=z(2).something(2);
        end%functions
    end%methods
end %class

but I want to use something like that:

classdef class
      properties
          x
          y
          z
      end % properties
      methods
          function obj = xxxx(obj,allproperties)
              obj.prop.1(1).value(1)=x(1).value(1);
              obj.prop.1(2).value(2)=x(2).value(2);
              obj.prop.2(1).time(1)=y(1).time(1);
              obj.prop.2(2).time(2)=y(2).time(2);
              obj.prop.3(1).something(1)=z(1).something(1);
              obj.prop.3(2).something(2)=z(2).something(2);
          end%functions
      end%methods
  end %class

is there any way to provide all properties without calling them seperatly.

thank you for now.

  0 Comments

Products

No products are associated with this question.

2 Answers

Matt J
Answer by Matt J
on 15 Nov 2012

The organization of your properties and data looks strangely complicated.

Regardless, though, you can use dynamic field names to access properties

    propnames={'x','y','z'};
    for i=1:length(propnames) 
     obj.(propnames{i}) = whatever
    end

  0 Comments


Answer by per isakson
on 19 Nov 2012

See documentation on:

  • meta.class class describes MATLAB classes and
  • meta.class.fromName. Return meta.class object associated with named class

  0 Comments


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today