how to clear variables when exit aplication?

Answers (1)

You can do
clear all;
or
clearvars;
Though when it leaves the button callback function it will clear everything anyway without calling clear. If you put that in a script rather than a function, it will clear variables in the base workspace.

6 Comments

Lukasz Jarod's "Answer" moved here:
i know what to use i just dont know where to use it , where is function that responds X button?
Again, I don't think you need to in most cases, but sometimes you do, for example if you have timers running, etc. If by X you mean the button on the title bar of your GUI instead of some button you created then you need to put clean up code in the CloseRequest() function. For example:
% --- Executes when user attempts to close figMainWindow.
function figMainWindow_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figMainWindow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
global vidobj; % Video camera object.
try % Save the current settings to our disk file.
SaveUserSettings(handles, true);
% Shut down the timer.
StopTimer(handles);
% Shut down the camera
try
delete(vidobj);
catch
end
% Get rid of variables from the global workspace so that they aren't
% hanging around the next time we try to run this m-file.
clear global;
delete(hObject);
catch ME
warningMessage = sprintf('Error in function figMainWindow_CloseRequestFcn.\nError Message:\n%s', ME.message);
set(handles.txtInfo, 'string', warningMessage);
WarnUser(warningMessage);
end
fprintf(1, '\n================= Exited figMainWindow_CloseRequestFcn. =================\n');
return; % from figMainWindow_CloseRequestFcn()
yes i want to clean my variables when i click X button on the title bar of GUI. I try to open figMainWindow_CloseRequestFcn but it give ma an error that file not found, maybe it had other name in 2014a???
Did you change figMainWindow so that it's the actual name of what your "Tag" property on your main GUI is? Probably not. What is the tag? Search your code for CloseRequestFcn - can you find it anywhere? What about the OpeningFcn?
i found it , but it didnt work i want it;/
is there any comend "shut down function"??
i have program, when it eneter to the function and i will exit the program function will stil execute and it will poput new window;/
I see no reason why your program will run again when you exit it. Perhaps did you double-click the run button, or hits F5 twice, when you launched it? Otherwise we'd have to see the code. All I know is that my programs don't run again once I shut them down.

Sign in to comment.

Categories

Asked:

on 14 Dec 2014

Commented:

on 28 Dec 2014

Community Treasure Hunt

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

Start Hunting!