MATLAB: How can i call class from a function

classMATLABoop

Hello,
i have a class with private properties and public methods to change the properties.
I want to call the methods of the class from a function in another m.File and change the methods.
At the end a function display should return a value. Something like this:
value=dispspeed(classcar);
value=10;
How can it be done?
Thank you in advance!

Best Answer

You should really have a look at the documentation of how you write classes in Matlab, starting for example here.
With the code below in a file called 'car.m', you can call aCar=car(3);aCar.showSpeed and it will disp the speed.
classdef car
properties
speed
end
methods
function obj = car(set_speed)
if nargin>0
obj.speed=set_speed;
else
obj.speed=0;
end
end
function obj = setSpeed(obj,set_speed)
obj.speed=set_speed;
end
function showSpeed(obj)
disp(obj.speed)
end
end
end