Class method is 'call by value' function, isn't it?
Show older comments
classdef myClass
properties
prop1
end
methods
function this_ = myClass(argin1_, argin2_) % constructor
this_.prop1 = argin1_ + argin2_;
end
function setter1(argin)
prop1_ = argin + 1;
end
function this_ = setter2(this_, argin)
this_.prop1 = argin + 1;
end
end
end
Matlab editor give me a error message for setter1 method.
It tells me that first input arg must be instance itself, and must be returned after manipulation.
This implies a class method of Matlab is a cal-by-value function.
I have very large fixed size array data as a property of my class,
and want to update a couple of elements in it many times very quickly.
so, definitely I want to avoid 'call by value' function as my method.
What would be a option for me,
Thank you.
Accepted Answer
More Answers (1)
per isakson
on 20 Jan 2022
0 votes
"It tells me that first input arg must be instance itself" Yes, Matlab requires that the instance is passed explicitly as in setter2. That applies to both handle and value classes.
"What would be a option for me" Use a handle class.
Categories
Find more on Class File Organization 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!