passing by reference an object in a function

18 次查看(过去 30 天)
I have a simple question about passing a variable by reference in Matlab. from the forums I have seen, there is no clear answer about that. From what I have understood( but may be I am wrong), let say that you have a function that take a table A and want to change in this table only the h line. if I do
function arg=myFunction(A,h)
A(h,:) = A(h,:)+2;
end
and call myFunction(A,3), A will not be changed. When specifying myFunction like this
function A=myFunction(A,h)
A(h,:) = A(h,:)+2;
end
and calling myFunction(A,3), A is changed. But from what I understood of what matlab does, matlab is copying A, then change in the copy the h lines of A, then provide output the copy. First question: Is this true or not? and if yes, is there a way to pass it by reference, which means directly modifying A. I know that by doing directly A(h,:) = A(h,:)+2; in the command line, I can obtain the same result. but on some applications, when A is very large, like Gibbs sampler, where i want to sample sequentially each line of A conditionnally to the other ones, it looks to me that copying for every call the large dataset A is inefficient. I am not an expert of matlab, please excuse if the question is stupid.
  1 个评论
Adam
Adam 2015-6-9
In general in Matlab everything is copied by value, yes. The link James gives below should be sufficient for your needs I think.
The other approach which is what I always use is to use Object-oriented programming.
Classes that are derived from the handle class are always passed by reference.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2015-6-9
编辑:James Tursa 2015-6-11
For inplace operations on data using functions, see this Blog by Loren:
Basically, there are rules you must follow for the function signature, and you must call the function from within another function.
Regarding your question about what MATLAB normally does for an arbitrary function argument, the basic answer is this:
If the function is a mex routine, MATLAB passes the address of the original variable (i.e., not a shared data copy). The mex routine is expected to treat all input arguments as read-only.
If the function is a m-file and the variable is not an OOP object derived from handle, MATLAB passes a shared data copy of the variable (i.e., a new variable but no data copy yet). If the function subsequently changes the passed variable, the variable is first unshared (i.e., a data copy takes place) and then the changes are made to this newly unshared variable.
If the function is a m-file and the variable is an OOP object derived from handle, MATLAB basically allows the function to make changes to the variable inplace (i.e., data is allowed to be altered inplace with no data copy).
Note that struct and cell array references (e.g., mystruct.field1 or mycell{3}) always create a temporary shared data copy of the field or cell element. So if you have this expression as an agrument a temporary shared data copy will always be passed, even to a mex routine.;
Loren's Blog talks about how to alter some of this behavior to get MATLAB to operate inplace in some circumstances.
  2 个评论
nazim regnard
nazim regnard 2015-6-9
编辑:nazim regnard 2015-6-9
Thank you very much both of you (james and Adam) for your answers. I am clear about the problem now. Adam I looked at the answer you wrote at this link
If possible I would like a last clarification. Objects that derive from class "handle" are passed by references. Does it mean that, in general, all other objects are not passed by references? I know that we can import java classes in Matlab. I did that in the past. Are java objects passed by reference or by copy to m-file functions ? I already used that in the past, and naively thought they were passed by references.
If not, for any java object "Object", if I define a class like
classdef myClass < handle properties Object end methods function h = myClass(Object) h.Object = Object ; end end end
I can pass by reference h in my function. Am I right?
Thank you very much for your time and the clarity of your answers.
Adam
Adam 2015-6-10
编辑:James Tursa 2015-6-11
I'm not sure about java objects to be honest as I very rarely use them. All other Matlab objects are passed by value, not be reference. In terms of memory allocation for taking copies though it isn't quite that simple. If the object is not actually altered within the function it is passed to then no new memory is allocated. Only if the object is edited does new memory get allocated for the hard copy.
But yes, if you derive a class from handle then every object of that class will be passed by reference.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by