MATLAB: Pass variable by reference to function

classhandlesMATLABooppass by referencepointerstructures

I have read that the only way to pass by reference with a function is if you pass a handle. Is there a matlab call to get the "handle" of a variable or structure?

Best Answer

While I think IA's answer is the better way to do it. If you really want to do what you are talking about you can create a subclass of handle.
classdef myClass < handle
properties
data
end
methods
function h = myClass(data)
h.data = data ;
end
end
end
Then defining a function (which could be a method) like
function myFunction(h)
h.data = h.data+2;
end
lets you do something like
h = myClass(0);
myFunction(h);
h.data
ans =
2
of course things like h+2 will not work unless you implement a plus method.