How can i call class from a function?
69 views (last 30 days)
Show older comments
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!
2 Comments
Rik
on 29 Jun 2018
Don't you mean something like this?
value=CarClassObject.dispspeed;
fprintf('The speed of the car is %.0f\n',value)
Or do you want to change the class itself and modify the method for all objects of that class?
Also, you should mark answers as accepted answer if they solved your problem. It is an easy way to say thank you to people investing their time in your problem.
Accepted Answer
Rik
on 29 Jun 2018
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
4 Comments
Rik
on 30 Jun 2018
You don't need to explicitly call the constructor. If you want to change the private properties, just change them inside a method. The showSpeed method doesn't return a value. Just use the getter I describe in the comment above. Then you can simply use the syntax I describe.
More Answers (1)
Steven Lord
on 29 Jun 2018
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
Up to this point I follow you. That's easy.
and change the methods.
I don't understand what you're asking here. I suspect you want to change the value of the property without having to return the modified object from the method. If so, you'd want your object to be a handle class. See this documentation page for a discussion of the difference between handle and value classes.
At the end a function display should return a value. Something like this:
value=dispspeed(classcar);
value=10;
If what I guessed about you wanting to use a handle class is not correct, can you explain in a bit more detail exactly how you want instances of your car class to behave?
2 Comments
Steven Lord
on 30 Jun 2018
The constructor for a class and methods of that class are each functions, and can be called just like any other function (with a few exceptions.) The main exception is that to call a method, at least one of the inputs must be an instance of the class for which the method is defined. [I'm ignoring Static methods for purposes of this discussion.]
The easiest way to access properties of an object inside a function is to use dot notation. Basically, inside a function that is not inside the class you can only access public properties. Inside a class method (which as I said above is a function) you can access private properties as well.
function displayObject(obj)
fprintf('Property a of the object: %d\n', obj.a);
end
If you want to return the property, just assign the property to a variable.
function y = returnPropertyA(obj)
y = obj.a;
end
Note that you need to refer to the variable obj. This is particularly important if a method accepts two or more instances of the class, like:
function y = plus(obj1, obj2)
y = obj1.a + obj2.a;
end
See Also
Categories
Find more on Construct and Work with Object Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!