properties and object oriented programming

2 Ansichten (letzte 30 Tage)
juliperde
juliperde am 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.

Antworten (2)

Matt J
Matt J am 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

per isakson
per isakson am 19 Nov. 2012
See documentation on:
  • meta.class class describes MATLAB classes and
  • meta.class.fromName. Return meta.class object associated with named class

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by