Is it possible to get access to variables within function handles?

조회 수: 11 (최근 30일)
Brando Miranda
Brando Miranda 2017년 4월 20일
댓글: Steven Lord 2017년 4월 21일
For example say I define:
c=3;
f = @(x) x + c;
is it possible to do
f.c
or basically get access to the variables within the function handle?

답변 (3개)

Steven Lord
Steven Lord 2017년 4월 21일
For debugging purposes, yes. The workspace field of the output of the functions function contains that information. The "Display Information About Anonymous Function Handle" example on that page is very close to the problem you wrote.
Again, I want to emphasize a note from that documentation page: " Note: Do not use functions programmatically because its behavior could change in subsequent MATLAB® releases. "
  댓글 수: 2
James Tursa
James Tursa 2017년 4월 21일
To make it clear, this is to examine the values only. You cannot change these values using this info.
Steven Lord
Steven Lord 2017년 4월 21일
Correct. To change the values, recreate the function handle.

댓글을 달려면 로그인하십시오.


Brando Miranda
Brando Miranda 2017년 4월 21일
c = 3;
f = @(x) x + c;
ff = functions(f);
ff.workspace{1}.c
  댓글 수: 1
James Tursa
James Tursa 2017년 4월 21일
To make it clear, this is to examine the values only. You cannot change these values using this info.

댓글을 달려면 로그인하십시오.


James Tursa
James Tursa 2017년 4월 20일
The only way I would be aware of would be a mex hack. E.g.,
c = 3;
f = @(x) x + c;
The "c" inside of the function handle "f" is actually a shared data copy snapshot of the "c" in the workspace at the time of the function handle creation. It is contained within the function handle itself. The only way to subsequently gain access to it would be to maintain the shared data copy of it at the workspace level (i.e., the "c" in the workspace), pass that to a mex routine, which could then hack into the variable to discover where the function handle "c" lives in memory. This procedure is not supported by any official API functions.
If you need to change the value of "c" in the function handle, it is probably best to just create a new function handle.

카테고리

Help CenterFile 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!

Translated by