- Are you aware of the difference between handle and value classes? From your code fragments, I conclude that you anticipate the behavior of a handle class. Thus, the class must inherit from the handle class.
- One must use some convention to distinguish between class properties and local variables. It is confusing to use the same name for a property and a local variable - to say the least. A property the shall always be referred to with the full name, obj.PropertyName, e.g. obj.Height.
Setting object properties from a class method
65 views (last 30 days)
Show older comments
Manish Makwana
on 11 Aug 2012
Commented: emre parlak
on 9 Jul 2020
Hi,
I'm exploring OOP with matlab and have the following code:
% method declaration
function Update(obj, Time)
obj.DeltaTime = Time; % saves time parameter to a class-wide property
obj.Height = obj.Height + obj.DeltaHeight; % new tank height
end
DeltaTime, Height are public properties of the class this sits inside. I call the method using the following syntax:
% method call
Update(TankA, 7)
There are other methods that use DeltaTime to calculate DeltaHeight. These all work perfectly fine. However, calling this method doesn't change the DeltaTime property, nor set Height to its new value. I don't get any errors thrown when the code runs, so have no idea why its not working as I expect it to. I don't know how to pass the values to the properties as method outputs either:
% using method outputs
function [DeltaTime, Height] = Update(obj,Time)
DeltaTime = Time;
Height = obj.Height + DeltaHeight;
end
Ideally I want DeltaTime, DeltaHeight to be private properties, and not manipulated directly from a separate file. Can someone please give me an idea what could be causing this?
0 Comments
Accepted Answer
per isakson
on 11 Aug 2012
Edited: per isakson
on 11 Aug 2012
Here is an example, which illustrates what I believe you try to do.
>> clear all, clear classes
>> tank_a = MyTankClass;
>> display( tank_a )
Height: 0.000000
DeltaHeight:
DeltaTime:
>> Update( tank_a, 17, 5 )
>> display( tank_a )
Height: 5.000000
DeltaHeight: 5.000000
DeltaTime: 17.000000
where
classdef MyTankClass < handle
properties ( Access = private )
Height = 0;
DeltaHeight
DeltaTime
end
methods
function Update( obj, time, height )
obj.DeltaTime = time;
obj.DeltaHeight = height;
obj.Height = obj.Height + obj.DeltaHeight;
end
function display( obj )
fprintf( 'Height: %f\nDeltaHeight: %f\nDeltaTime: %f\n' ...
, obj.Height, obj.DeltaHeight, obj.DeltaTime )
end
end
end
5 Comments
Steven Lord
on 8 Jul 2020
See this documentation page for a description of the difference between handle classes and value classes.
emre parlak
on 9 Jul 2020
i used varargin and nargins and some if else statements to do it. it is not that of a big deal i guess. thanks for the help
More Answers (1)
Image Analyst
on 11 Aug 2012
What is obj? Is it some other object? It's not the same object is it? If it is, why aren't you simply doing:
function Height = Update(Time)
Height = Height + Time;
end
You don't even need DeltaTime since it's the same as Time that you passed in.
2 Comments
Robert Jack
on 17 Apr 2018
I don't understand why this is the accepted answer. In my view the answer by per isakson is better. I'm troubled why an mvp is asking what 'obj' is when the context is clearly that of class properties.
See Also
Categories
Find more on Debugging and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!