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

1 view (last 30 days)
Matt Kutcher
Matt Kutcher on 26 Nov 2021
Commented: Matt Kutcher on 27 Nov 2021
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
@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)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!