Borrar filtros
Borrar filtros

Clear function works in command window but not in script file

3 visualizaciones (últimos 30 días)
Matt
Matt el 31 de Ag. de 2015
Comentada: Titus Edelhofer el 1 de Sept. de 2015
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?

Respuestas (2)

Walter Roberson
Walter Roberson el 31 de Ag. de 2015
Leave out the (), just
clear varname
  7 comentarios
Matt
Matt el 1 de Sept. de 2015
Thank you for your answer guys. I am still struggling on this simple task however. I've tried several combination and eventho I understand the principle of evalin and assignin, I cannot get my head around this.
varname=strcat('time',num2str(get(popupmenu1, 'Value')))
evalin('base', varname)
clear varname
The first line should give me something like time1, time2, or time3. I am then getting the value out of the workspace with evalin, and then clearing it, but it doesn't work.
Titus Edelhofer
Titus Edelhofer el 1 de Sept. de 2015
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]);

Iniciar sesión para comentar.


Titus Edelhofer
Titus Edelhofer el 1 de Sept. de 2015
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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by