필터 지우기
필터 지우기

How can I remove a variable from the parent workspace while a function is using it?

조회 수: 2 (최근 30일)
I work with fairly large datasets, so the fact that they get duplicated when a function accepts them as an input can be a problem given 32bit memory constraints.
I have started using scripts instead of functions in order to save on memory usage, but this has caused additional problems, because now their is no distinction between the workspaces and I have to be very careful not to overwrite utility variables (such as x, y, t, n, etc.)
I would really like to pass a variable as a function input such that there are not two copies floating around the system.
How can I remove the variable from the parent workspace (or possibly act directly on only the parent workspace variables that are specified as functional inputs?)
Thanks, Sean

채택된 답변

the cyclist
the cyclist 2011년 12월 14일
Are you changing the value of the variable after it is passed in? If not, then a copy is never made.
If it is changed, you could define the variable as global to ensure having only one copy.
  댓글 수: 3
Sean
Sean 2011년 12월 14일
I have a natural aversion to using global variables, because it is so easy to cause problems that way, but perhaps this is one of the few 'correct' times to use a global...
The reason I am not using nested functions is because I do not want to risk overwriting other variables in the parent workspace.
Walter Roberson
Walter Roberson 2011년 12월 14일
The only variables that can be overwritten are the ones that are initialized before the nested routines are declared.
function parent
shared1=[];
function nested1
shared1='hello'; %affects shared variable
i=15; %local variable because it was not initialized before nested routine
end
i=32; %local variable because it was not initialized before nested routine
nested1(); %will affect shared1 but not i
end

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 14일
To answer the original question:
evalin('caller', ['clear ', inputname(3)])
where the 3 here should be changed to the parameter number of the variable in the calling sequence.
Crashes due to doing this would not surprise me.
  댓글 수: 1
Sean
Sean 2011년 12월 14일
Walter,
Thanks for the answer. I was unfamiliar with the 'evalin' command. I can see that it would work for what I was asking, but can also potentially be badly abused. :)
I would mark you as the accepted answer, but I already marked 'the cyclist' as such.
Thanks again,
Sean

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

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by