Is there a way to remove all appdata?

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

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

Thank you for the rapid response. I will give that a try.
In regards to the second question, I am not sure if I can use that since I need access to the data from multiple GUI's. I have been using setappdata but I have been saving it to '0' instead of 'handles.figure1' for that reason. Is that OK then? Or, should I still save it to the current figure?
Once again, thanks for the response... I tried what you suggested, but it keeps removing something by the name of "OpenFig_gifE2x0tset_ecafretnI_OTDAMM_SINGLETON" which results in an error message. How can I bypass that file so I don't remove it?
Thank you Sean.
I'm still not clear how SETDIFF would help bypass that. Should I save the initial appdata then compare it to the final appdata; then, when it is time to remove what I added just removing whatever changed from beginning to end?
What I am not sure is if the "appdata = OpenFig_gifE2x0tset_ecafretnI_OTDAMM_SINGLETON: 229.0034" is unique for my GUI or would it change from machine to machine. By any chance, do you know what that specific appdata is?
I am still in the process of learning Matlab, so thank you for your time and for being patient with me.
No clue. That's why I recommend avoiding appdata altogether ;)
Why not just pass the handles structure into your additional GUIs? Then they'll have access to handles.figure1 which will in turn give them appdata stored there.
I think I am passing them. When I move from GUI to GUI, I use something like:
FIXED_termination('FIXED_termination', handles.figure1);
I had trouble getting the code to work though, since I was calling additional GUI's from the sub GUI's. I think I may not have been using the passed handles right, so I might try that again.
I got it to work using the method mentioned above, but I am having to ignore that specific appdata (the problem is that I am not sure if that is unique to my program/system or not).
Again, thank you so much for your time and effort. Should I leave the question unanswered, to see if someone else has a different idea?
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.
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

Products

Community Treasure Hunt

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

Start Hunting!