|
1 |
| -function saveGUIstate(handles) |
| 1 | +function saveGUIstate(h) |
| 2 | +% Write better documentation here <- |
| 3 | +% |
| 4 | +% Input is a structure array of handles from programmatic or GUIDE GUIs |
| 5 | +% Output is a saved *.mat file with the specified properties |
| 6 | +% |
| 7 | +% Assumes all structure arrays with the same field name are of the same |
| 8 | +% object type (e.g. 3 sliders stored as h.slider(1), h.slider(2), ...) so |
| 9 | +% the user can utilize the return of set/get natively. |
2 | 10 |
|
3 | 11 | propertystruct = initializeproperties();
|
| 12 | +outfilename = 'savedState.mat'; |
| 13 | + |
| 14 | +objectlist = fieldnames(h); |
| 15 | + |
| 16 | +for ii = 1:length(objectlist) |
| 17 | + objtype = get(h.(objectlist{ii}), 'Type'); |
| 18 | + if iscell(objtype) |
| 19 | + % Structure array length is non-singular, so get returns a cell |
| 20 | + % array where each cell corresponds to the property of an |
| 21 | + % individual object in the array. |
| 22 | + objtype = objtype{1}; |
| 23 | + end |
| 24 | + fieldtest = isfield(propertystruct, objtype); % See if our field exists |
| 25 | + |
| 26 | + % If we have a uicontrol object, adjust the object type string to |
| 27 | + % include the 'Style' property as well so the properties to output |
| 28 | + % structure can be addressed directly |
| 29 | + isuicontrol = strcmpi(objtype, 'uicontrol'); |
| 30 | + if isuicontrol |
| 31 | + uicontrolstyle = get(h.(objectlist{ii}), 'Style'); |
| 32 | + if iscell(uicontrolstyle) |
| 33 | + % Structure array length is non-singular, so get returns a cell |
| 34 | + % array where each cell corresponds to the property of an |
| 35 | + % individual object in the array. |
| 36 | + uicontrolstyle = uicontrolstyle{1}; |
| 37 | + end |
| 38 | + |
| 39 | + % isfield doesn't support nested fields, so we need to test the |
| 40 | + % uicontrol substructure directly |
| 41 | + fieldtest = isfield(propertystruct.uicontrol, uicontrolstyle); |
| 42 | + end |
| 43 | + |
| 44 | + % Field exists, and there is data to save, save it |
| 45 | + if fieldtest |
| 46 | + % Need a in/else statement because you can't use dynamic field |
| 47 | + % references on nested structures |
| 48 | + if isuicontrol |
| 49 | + propstopull = propertystruct.uicontrol.(uicontrolstyle); |
| 50 | + else |
| 51 | + propstopull = propertystruct.(objtype); |
| 52 | + end |
| 53 | + |
| 54 | + if ~isempty(propstopull) |
| 55 | + if iscell(propstopull) |
| 56 | + for jj = 1:length(propstopull) |
| 57 | + asdf = 1; |
| 58 | + end |
| 59 | + else |
| 60 | + asdf = 1; |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | +end |
4 | 65 | end
|
5 | 66 |
|
6 | 67 | function [propertystruct] = initializeproperties()
|
7 | 68 | % Initialize structure containing the default properties to save.
|
8 | 69 | % Fieldnames of propertystruct should match the 'Type' property of the UI
|
9 | 70 | % object to save. If the object has multiple types (e.g. uicontrol), then
|
10 | 71 | % nest the types under their overall object type (e.g. uicontrol.edit).
|
| 72 | +% |
11 | 73 | % Strings (single or cell array) must match the property name(s).
|
12 | 74 |
|
13 |
| -propertystruct.figure = 'Position'; |
| 75 | +propertystruct.figure = {'Units', 'Position'}; |
14 | 76 | propertystruct.axes = ''; % Nothing to save currently
|
15 | 77 | propertystruct.uipanel = ''; % Nothing to save currently
|
16 | 78 | propertystruct.uitabgroup = ''; % Nothing to save currently, introduced in R2014b
|
|
0 commit comments