Object composition and updating property

1 visualización (últimos 30 días)
Thierry NOVO
Thierry NOVO el 6 de Jul. de 2013
I'm writing my first program with the OOP method during several weeks and i face with an issue with composition object and update properties of object . Here is the definition of my classes: First the class Mat:
classdef Mat < handle
properties
z_left
z_right
dz
end
properties(Dependent)
z
end
methods
% Constructor
function newMat = Mat(z_left, z_right, dz)
if nargin == 3
newMat.z_left = z_left;
newMat.z_right = z_right;
newMat.dz = dz;
end
end
function z = get.z(newMat)
z = (newMat.z_left:newMat.dz:newMat.z_right)';
end
function newMat = set.z_left(newMat,value)
newMat.z_left = value;
end
end
end
I use a function sysmeca to create an object composed of two instanciation of the class Mat:
function [ obj ] = Sysmeca( varargin )
if (nargin < 1), error('Wrong number of input arguments.'); end
%
n = length(varargin);
obj = cell(1,n);
for k=1:n
obj{k}=varargin{k};
end
end
Then the class Model:
classdef Model < handle
properties
obj;
end
properties(Dependent)
z
end
methods
function newModel = Model(obj)
newModel.obj = obj;
end
function z = get.z(newModel)
L = 0;
z = 0;
for k=1:length(newModel.obj)
z = [z ; newModel.obj{k}.z + L];
newModel.obj{k}.z_left = L;
L = newModel.obj{k}.z_left;
end
end
end
end
Here is finally the main script:
clc;
close all;
clear all;
clear classes;
cm = 1e-02;
Mat1 = Mat(0,5*cm,0.1*cm);
Mat2 = Mat(0,2*cm,0.2*cm);
sys = Sysmeca(Mat1,Mat2);
Part = Model(sys);
My problem is that i wish to update property z_left in the objects Mat1 and Mat2 inside the get.z method in the class Model by the instruction newModel.obj{k}.z_left = L;, but it doesn't work. I try to implement a set method in the class Mat definition to update the property z_left, but no success.
Can somebody help me to understand how i can update property object which is part of an object composition ?? Thanks a lot in advance.
  1 comentario
Matt J
Matt J el 6 de Jul. de 2013
Editada: Matt J el 6 de Jul. de 2013
Works fine for me. I get no error messages with or without the explicitly defined set.z_left (which looks unnecessary).

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Object Save and Load en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by