how to clear variables when exit aplication?
Show older comments
How can i clear all variables on click X button???
Answers (1)
Image Analyst
on 14 Dec 2014
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
Image Analyst
on 14 Dec 2014
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?
Image Analyst
on 14 Dec 2014
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()
Lukasz Jarod
on 14 Dec 2014
Image Analyst
on 14 Dec 2014
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?
Lukasz Jarod
on 15 Dec 2014
Image Analyst
on 28 Dec 2014
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.
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!