Clear function works in command window but not in script file
이전 댓글 표시
Hello everyone,
I am facing a weird problem here with the CLEAR function. I am generating a string using the following function:
varname=strcat('time',num2str(get(popupmenu1, 'Value')))
I am then using the following function:
clear (varname)
However, I do not get any error, but the variable is not deleted. On the other hand, when I just type the above command directly in the command window, the variable is deleted!
How comes?
답변 (2개)
Walter Roberson
2015년 8월 31일
Leave out the (), just
clear varname
댓글 수: 7
Matt
2015년 8월 31일
Walter Roberson
2015년 8월 31일
Which workspace are you looking at? clear is going to remove from the current workspace, and functions have their own workspace. I speculate that you might be attempting to clear from the base workspace. If so then you need to use
evalin('base', 'clear varname');
Matt
2015년 9월 1일
Titus Edelhofer
2015년 9월 1일
Hi Matt,
the evalin instructs MATLAB to evaluate the string in another workspace (usually the base workspace). So the line that Walter wrote can be called basically anywhere you like, it will always do the same and clears the variable varname from the base workspace ...
Titus
Titus Edelhofer
2015년 9월 1일
And you were right, you should have used the () ...
Matt
2015년 9월 1일
Titus Edelhofer
2015년 9월 1일
As I wrote below, this should work
varname=strcat('time',num2str(get(popupmenu1, 'Value')))
% get the data from base workspace
data =evalin('base', varname);
% clear the variable
evalin('base', ['clear ' varname]);
Titus Edelhofer
2015년 9월 1일
You can either use clear as function as you did, or use string concatenation. Since you need to use evalin anyway, string concatenation is not as bad as usual:
Summarizing, adding the following line after
varname=strcat(...)
should do:
evalin('base', ['clear ' varname]);
Titus
카테고리
도움말 센터 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!