GUI: How to save figure properties to files?

7 views (last 30 days)
Hi all, I would like to save figure properties(legend,background colors...) to some kind of files, so that I can recall them later. I used guidata(hObject,handles) to pass the figure properties from one callback function to another: save_setup_Callback(hObject, eventdata, handles) Here's my code:
function save_setup_Callback(hObject, eventdata, handles)
configuration.x_axis=get(handles.x_axis,'String'); % save marked items in listboxes
configuration.y_axis=get(handles.y_axis,'String');
[FileName,FilePath]=uiputfile('configuration.mat','Save Configuration','Configuration');
if FileName == 0
msgbox('You have canceled saving the file (.mat)!','Warning: no files saved','warn');
else
file = strcat(FilePath,FileName);
save(file);
save(file,'-struct','handles2','legend','-append') % handles2 includes the figure properties
But then I got this error in command window when executing:
Error using save
The argument to -STRUCT must be the name of a scalar structure variable.
I personally think it's because legend is not a structure, but a ''1x1 Legend''. Also I'm not sure if ''Legend'' can be written into a .mat file. So how can I save such format to files? And in which format?
Thank you in advance :)

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jan 2016
When you use -struct you can name only one variable, which will be broken up and its fields will become variables in the .mat file.
  2 Comments
dyyyyyl
dyyyyyl on 18 Jan 2016
Thanks for your reply :) So maybe it should not be '-struct' here? What format should it be then?
Walter Roberson
Walter Roberson on 18 Jan 2016
legends can be written to .mat files, but are you trying to save the legend itself or the properties of the legend (e.g., because you want to make another legend that looked like the first one did)?
Either way you have the problem that you do not have a handle to your legend, not unless you stored it inside handles.
And your handles2 is appearing out of no-where.
If you are wanting to save items out of your handles structure, extract the items into individual variables and save() naming the variables, with no '-struct' option. This could include saving actual graphics items.
If you are wanting to save the properties of items, then use get() on the items to extract the properties into a structure. If you are doing that for multiple items, create a structure for each and save() naming the variables, with no '-struct' option. You probably will not want to extract all of the properties, only the properties that are useful for your later purposes.
If you are wanting to save the properties of one item, each property becoming an individual variable in a .mat file, then use get() on the item to extract the properties into a structure. Then use save() using '-struct' followed by the name of the (one) structure variable. This would create one variable in the mat file for each field in the structure.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!