anyway to clear variable in Matlab workspace from app?

Hi:
is there anyway to clear variable in Matlab workspace from app designer?
an workaround I know is to use 'assign' function, to assign an empty value to that variable. but I want know how to clear it.
Thanks!
Yu

2 Comments

There should be no need to clear variables from any workspace from within a different workspace. If there is a need to do this, it raises red flags that something is being done in a bad way. Could you explain why you need to clear an external workspace?
There is rarely a need to clear variables from within a function but sometimes it useful to do so. There should never be a need to clear variables in a different workspace. Without knowing more about what you're doing, the best help I can offer is trying to clean up any reasons why someone would need to do this in the first place.
What workspace is the variable in (the base workspace?), how did it get there, and why does it need to be cleared from within a function (which has its own workspace).

Sign in to comment.

 Accepted Answer

evalin('base', 'clear VARIABLENAME')

11 Comments

Am I wrong to think there's no reason to ever do this? I guess I could imagine a circumstance where a large variable in another workspace is consuming a lot of memory and needs cleared after it's useage is complete.
It has been known for people to use the existence of a variable as a state flag. Even people who should know better sometimes slip into that, such as creating an environment variable whose existence signifies that some particular debugging is to be done.
Thank you Walter, this is exactly what I need.
Adam: thank you the same. I need this feature to translate data between workspace and appdesigner.
Bests,
Yu
Dear all,
I am trying to do the same but it does not worl.
Any help?
thank you
Massimo
here my code:
function DeleteVarButtonPushed(app, event)
%evalin('base','clear all')
dasalvare=evalin('base',app.PLUTODropDown.Value)
evalin('base', 'clear dasalvare')
end
dasalvare=evalin('base',app.PLUTODropDown.Value)
The app.PLUTODropDown.Value part will be evaluated in the workspace of the current function. Whatever character variable (or scalar string) it returns will be evaluated in the base workspace because of the evalin(). You store the result of the evaluation in dasalvare in the workspace of the current function.
Then you tell the base workspace to clear dasalvare . As outside observers, we have no reason to expect that a variable named dasalvare exists in the base workspace at this time. We only know of the dasalvare that you created in the workspace of the current function -- and since you leave the current function without somehow saving the dasalvare anywhere, there would be no need to clear it in the current workspace as the current workspace will disappear when the function returns.
Hi Walter,
thanks, I understand the point but I do not undestand how to make it work .
app.PLUTODropDown.Value This selection is a vector already in the base workspace. For example the output is the Y.
so when I write : dasalvare=evalin('base',app.PLUTODropDown.Value) I am expecting that dasalvare=Y
and therefore when i write: evalin('base', 'clear dasalvare')
I am expecting the action to take place in base ws where the action clear acts on the variable Y.
Where am I wrong?
The first step definitly since if I write in the code evalin('base', 'clear Y') then it works.
But I want that the dropdown menu reading the vector in the base WS pass its values to the clear function acting in the base WS. I underrstad I am missing ht econnection between the different space but ...
The reason why I do not get it is because the action below works fine i.e.
I read fromo two dropwodn two vector from the base ws and then I make an operation and store it in the base ws.
function DIVIONEButtonPushed(app, event)
Y=evalin('base',app.PLUTODropDown.Value)
X=evalin('base',app.PIPPODropDown.Value)
app.risultato=Y./X;
assignin('base','y_div_x',app.risultato)
end
thank yor explaining
Supposing that app.PLUTODropDown.Value contains the scalar string object "Y", then
dasalvare=evalin('base',app.PLUTODropDown.Value)
would be the same (for this purpose) as if you had evaluated
dasalvare=evalin('base',"Y")
which would look inside the base workspace for a variable named Y and would store the value of that variable into the variable dasalvare in the current workspace (the workspace of the function callback.) For example if Y contained numeric 123 then dasalvare in the local workspace would contain numeric 123. dasalvare would not contain the name Y, it would contain the value of Y .
evalin('base', 'clear dasalvare')
would then ask the base workspace to clear the variable named dasalvare but there is no such variable in the base workspace. This command would not ask the base workspace to clear Y -- remember dasalvare does not even contain the name Y, it contains the value stored in Y . And besides, you are using command/function duality so the call would be equivalent to
evalin('base', "clear('dasalvare')")
The value of dasalvare is not used.
Maybe you want something like
dasalvare = app.PLUTODropDown.Value;
evalin('base', "clear " + dasalvare)
that would have the potential to execute "clear Y" inside the base workspace.
Hello Walter,
thank you.
Your explanation is clear and the last sugestion works.
I do not know then why the command evalin('base', 'clear app.PLUTODropDown.Value') does not work. Should't it be similar to yours? Definitly not, since this does not work but I have tried before when I uderstood that the problem was the one you said. (yor first explanation)
the app.PLUTODropDown.Value gives me the name of the selected variable from the dropdownmenu. For example it returs 'field'. So it has the name of the variable to delete.
I would also like to ask you, how can I list in the dropdown the variable that are created within the matlab.app application and not those that are in the base WS? Probably the same roputine I am using would work with the proper indication of the proper workspace that I do not find how it is called.
To update the dropdown menu with the variable from base WS I use the routine:
function update_menu(app, ~, ~)
vars = evalin('base', 'whos');
cell_array = cell(size(vars));
for i=1:length(vars)
cell_array{i} = vars(i).name;
end
app.PLUTODropDown.Items = cell_array;
app.PIPPODropDown.Items = cell_array;
end
function results = UIFigureCloseRequest(app, event)
% Close request function: UIFigure
stop(app.menu_timer);
delete(app.menu_timer);
delete(app)
end
Thank yoy very much as you explanation poited me tho the rid mind direction, but now the problem is finding the right command combination based on their logic.
Massimo
Please see https://www.mathworks.com/help/matlab/matlab_prog/command-vs-function-syntax.html to get more information about why evalin('base', 'clear app.PLUTODropDown.Value') is not going to work.
I would also like to ask you, how can I list in the dropdown the variable that are created within the matlab.app application and not those that are in the base WS?
You can use properties to find the list of variables for the app object.
You will find that in practice most of them should not be eligible for clearing.

Sign in to comment.

More Answers (1)

To clear variable use 'clear' function instead, ex:
clear('v')
where 'v' is variable name.

Categories

Community Treasure Hunt

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

Start Hunting!