Is there a way to remove all appdata?

13 views (last 30 days)
I am saving all data from multiple GUI's (created with GUIDE) using "setappdata" into the root directory (0). I would like to remove all saved data when I close the main GUI that controls the other GUI's, without individually removing each one. What is the best way to do this?
I found the following thread, but I am still unclear of the actual solution: http://www.mathworks.com/matlabcentral/newsreader/view_thread/114248
Also, as a side note. Is it good programming practice to save data in the root directory? Or should I be doing that a different way?
Thank you in advance for any help provided.

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Jul 2013
Edited: Sean de Wolski on 9 Jul 2013
Well obviously you could just restart MATLAB. But we don't want you to have to do that!
So:
appdata = get(0,'ApplicationData')
Returns a structure with all of the Application data. You can loop over and remove it.
appdata = get(0,'ApplicationData')
fns = fieldnames(appdata);
for ii = 1:numel(fns)
rmappdata(0,fns{ii});
end
appdata = get(0,'ApplicationData') %make sure it's gone
To answer your second (and arguably more important) question, yes - this is a bad practice (That's not to say it's not exactly how I did it for my first 10ish GUIs in grad school...)
The reason it is a bad practice is because of exactly what you're seeing. You close the figure an dnow you have all of this orphaned appdata hanging around. It could be really bad if some of this was big arrays consuming memory with no use. It could also be bad if the next time your figure opens, it's preinitialized because some of this orphaned data is now being detected by the getappdata calls in your new figure.
So, how do you work around it. Use setappdata to set the application data to the main figure of your GUI. Now, when your figure is closed - poof! it's all gone.
To do this inside of GUIDE the commands will be:
setappdata(handles.figure1,'Sean',pi) %
Where the string figure1 is the 'Tag' of the figure.
This has some other benefits as well, including the fact that it is harder for an external process to change this data. I.e. anything can easily touch the root, but the handle of the figure is more obscure).
  8 Comments
Sean de Wolski
Sean de Wolski on 10 Jul 2013
One thing you could do to make sure you only destroy your own appdata would be to prefix it with something obscure
David_data
David_axesHandle
David_Time
Now you could use strncmp to remove only things that start with David_. Once again, all of this is a bandage for not storing the information in the figure's appdata.
David (degtusmc)
David (degtusmc) on 10 Jul 2013
That is a good idea. All my appdata already starts with Return_.
I'm going to try to store the information in the figure's appdata as you suggested; but at least I have a way to bypass the other issue we discussed earlier.
Thanks again!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!