why does UIOPEN opens my GUI two times!

7 views (last 30 days)
Geert
Geert on 20 Feb 2015
Edited: Adam on 20 Feb 2015
To clearify my problem I've used GUIDE to make a simple GUI. I've made two pushbuttons. One to save data using uisave , and one to load data using uiopen. I save the handle variables in a file with the first button. But when I open this file with the second button, it will open my GUI two times!! The code looks like this:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
uisave('handles');
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
uiopen();
I would really like to open my GUI only one time if I open a file. Or that it just loads the data into the already opened GUI. Looks like I'm doing something very stupid here... (when i just use uisave(); I have the same problem.
Geert

Accepted Answer

Adam
Adam on 20 Feb 2015
Loading an arbitrary 'handles' structure into a GUI to overwrite that of the GUI is not at all a good thing to do. I cannot think of any situation in which this is a good idea.
If you just want to save data then extract it from handles (if that is where you stored it) and save e.g.
data = handles.data;
uisave( 'data' );
That may not be what is causing your GUI to open twice, but the handles struct is a special struct which contains all the UI components for your GUI so loading it in may well cause the GUI to open again.
Either way, just don't do it! Never replace the handles structure in a callback with a different one!
  2 Comments
Geert
Geert on 20 Feb 2015
Thanks for the answer, but it won't be an arbitrary 'handles struct.
I tried to do it, because my original program calculates a certain map from external data. This calculation takes about an hour, and then the program asks to save it. I wanted to save 'every setting' so next time I need this map, I can load the handlesstruct and directly move on with the map 'the way I left it'.
Otherwise I probably need to name every single variable in the struct when I use uisave....
Adam
Adam on 20 Feb 2015
Edited: Adam on 20 Feb 2015
You can save your calculation and any data needed for it into some other struct with as many field as you like e.g. 'data' as mentioned above, attach that struct to handles for the lifetime of the GUI and then extract it from handles to save (as shown above).
That will allow you to separate all your data from the ui components that are automatically stored in the handles structure.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps 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!