Works fine for me. I get no error messages with or without the explicitly defined set.z_left (which looks unnecessary).
Object composition and updating property
1 次查看(过去 30 天)
显示 更早的评论
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.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Methods 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!