Clear function works in command window but not in script file
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Walter Roberson
2015-8-31
Leave out the (), just
clear varname
7 个评论
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!