How can I create a class method which can be called to modify various class properties repeatedly

6 views (last 30 days)
Hello! I have a class with a number of properties, and I want to write a method which can be called within the class and use properties as arguments in order to change the value of the input properties. Something like this.
classdef myClass < handle
properties
property1
property2
property3
property4
end
methods
function output = myFunction(input)
output = input;
end
% constructor
function object = myClass
% call the same function to modify various propeties
object.property1 = myFunction(object.property2);
object.property3 = myFunction(object.property4);
end
end
end
I know this code doesnt work and I have been using a handle class so my methods can modify properties. I know that the first argument generally has to be the object but I have no use for it in the function. Is there a way to pass properties and modify them similar to above? Any help would be great thanks!
  3 Comments
Matt Kutcher
Matt Kutcher on 27 Nov 2021
Ok maybe it would have been more clear if I posted the class I’m actually using. Essentially what I’m trying to do is pass a property to a method which will perform some calculations and assign it to another property. I don’t have a great understanding of OOP in MATLAB, but I thought the constructor method is used to create an instance of the class then when I use the object.property notation I can modify the properties of that I object. It that right?
Matt Kutcher
Matt Kutcher on 27 Nov 2021
@Chris I actually just managed to figure it out! I had tried previously using a static function but when calling it I never used the obj. notation in front to the function handle. Its now working how I want, thanks for the help!

Sign in to comment.

Answers (0)

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!