Can MATLAB pass by reference?
이전 댓글 표시
How does MATLAB deal with this?
댓글 수: 1
E65EnyOwC
2018년 9월 14일
Check out: "How MATLAB Allocates Memory" in the Matlab help files
채택된 답변
추가 답변 (4개)
Marco
2012년 7월 18일
1 개 추천
Is this also true for nested function calls? Cuz it seems like it only works for direct function calls. In my GUIDE gui I have a function (call it Fn_takesHandles) that takes in the figure handles and updates the axes, when called by callback functions directly works just as intended. However, I run into problems when the stack frame is not one-to-one. A callBack function calls a helper function called Auto which in turn calls Fn_takesHandles. In this case it does not work as intended. Any changes made to the figure handles by Fn_takesHandles is not persistent. How would you go about solving this problem?
Is there any way to declare pointers in matlab? Any help would be appreciated!!
댓글 수: 1
Walter Roberson
2012년 7월 19일
편집: Walter Roberson
2018년 9월 14일
There is no way to declare pointers in MATLAB (at least not for calling MATLAB functions; there are ways to copy around pointers that have been created at the C / C++ level.)
Jason Climer
2017년 6월 20일
Is this determined during the JIT compilation or as needed, i.e., is the copy of x made upon the function call or when the program executes
x(2) = 2;
?
댓글 수: 1
Walter Roberson
2017년 6월 20일
It is generally determined when the program executes, as it is not always possible to tell ahead of time whether a value is shared or not. Especially when you consider the hidden effect of assignin() .
Also, overloading can happen at run-time: the current directory or path of an operation at the time a routine is first JIT'd is not necessarily going to be the same as during a later operation.
There are cases where MATLAB does enough analysis to establish that "update in place" can happen; I do not know enough about the mechanics of that to say how it is done taking into account overloading.
Mandeguz
2017년 6월 22일
0 개 추천
How about when writing MEXs? Can one pass by reference within the computational routine and pull those values back to the main MATLAB code that called the MEX?
댓글 수: 4
Jan
2017년 6월 22일
All variables are provided by reference in a Mex function: you get only pointers to the inputs. The documented MEX functions do not allow to modify the inputs directly, but it is required to create a data copy at first. But of course in the C level there is a way to maniipulate the (potentially shared data) directly. There are several undocumented methods to handle shared data copies, see e.g. https://www.mathworks.com/matlabcentral/answers/231824-issue-with-mex-function#comment_302416 . Search for "mxCreateReference" and "mxCreateSharedDataCopy" in the net to find more details.
James Tursa
2017년 6월 22일
Also, keep in mind that struct and cell array references are evaluated as shared data copies at the caller level before the argument is passed on to the mex routine. E.g.,
x = something;
mymex(x); <-- The address of x is passed into the mex routine
y.a = something;
mymex(y.a); <-- The address of a temporary shared data copy of y.a is passed into the mex routine
z{1} = something;
mymex(z{1}); <-- The address of a temporary shared data copy of z{1} is passed into the mex routine
If the struct or cell element hasn't been created yet (i.e., is actually NULL), then a temporary empty double variable is created on the fly and the address of that is passed in.
So, when working with struct and cell arrays, it is more efficient to pass them in as whole variables and extract the element pointers inside the mex routine to avoid those temporary shared data copies.
James Tursa
2018년 9월 14일
Also note that as of R2015b, MATLAB passes arguments to mex routines as shared data copies (it used to pass them by reference).
James Tursa
2019년 1월 10일
And as of R2018a, complex variable arguments to mex routines essentially get deep data copied in both directions if you use the -R2017a compilation model.
Edgar Sanchez
2018년 12월 25일
0 개 추천
Maybe you can use global variables
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!