Problem with uiwait and uiresume
Mostrar comentarios más antiguos
I'm trying to pass a structure from a one window to another in my GUI. To start, in the opening function, I pause the ui, using uiwait(handles.LoadWindow) (LoadWindow is the name of the child window) in order to wait for the user to input some data.
After the user does this and presses the OK button I create the structure "File" and add some fields as follows:
function OKButton_Callback(hObject, eventdata, handles)
File.Machine = get(handles.popupmenu1, 'Value');
File.Test = 'Test';
handles.output = File;
close(handles.LoadWindow);
I've added the structure to handles.output which should be returned to the main window. The child window (LoadWindow) is then closed.
At this point my understanding is that the outputFunction is automatically called:
function varargout = LoadWindow_OutputFcn(hObject, eventdata, handles)
uiresume(handles.LoadWindow)
varargout{1} = handles.output;
When I run the GUI, I get the error:
??? Attempt to reference field of non-structure array.
Error in ==> LoadWindow>LoadWindow_OutputFcn at 74
uiresume(handles.LoadWindow);
When I enter debug mode at this step my handles value is [ ]. Going back farther my handles object is fine when I press the OK button and it contains all the expected fields. It's at line 211 of gui_mainfcn.m where my handles object gets assigned the value [ ].
When I remove the uiwait/uiresume functions the code works, but it doesn't wait for the user to enter data before returning control to the main window.
I'm using Matlab r2006b on Windows 7.
Any help would be greatly appreciated.
Thanks,
Lukas
Respuestas (1)
You need to update the structure with the GUIDATA function.
guidata(gcbf,handles)
11 comentarios
Lukas
el 26 de Sept. de 2012
Matt Fig
el 28 de Sept. de 2012
Lukas comments:
"Matt,
I updated my handles structure at the end of the OK_button_callback with the call: guidata(handles.LoadWindow, handles)
I also added a CloseRequestFcn that gets called when I close the window. All that works, except I still have the problem with the OutputFcn I described before. I don't think that it's a problem with guidata, because the handles object is completely overwritten in gui_main.m
Any other help would be greatly appreciated.
Thanks,
Lukas"
Hi Lukas, I am sorry I missed your previous comment.
So let me ask you to be a little more clear on what is going on. Say you have two GUIs, GUI1 and GUI2. GUI1 is open and you push a button which triggers GUI2 to open, correct? Then GUI2 has some uicontrols or collects some data that you want to pass back to GUI1, is this right so far?
Here is a quick and dirty way to do it:
In GUI2, once the data is collected, set the userdata property of the root object to the data you want stored. Then in GUI you can simply retrieve it.
set(0,'userdata',mydata) % In GUI2
mydata = get(0,'userdata'); In GUI1
Lukas
el 28 de Sept. de 2012
Lukas
el 28 de Sept. de 2012
The OutputFcn is there to return anything you want (such as the structure containing the handles to all the GUIs objects) once the figure window is finished opening. It is not called again after that....
The more typical approach is this (for guide GUIs):
From inside GUI2, access the guidata for GUI1 by something like this:
gd = guidata(GUI1handle); % Get app data from GUI1
Now add to that structure:
gd.myGUI2data = rand; % Whatever...
% Now we store the updated structure in GUI1:
guidata(GUI1handle,gd) % Store in GUI1 appdata
Now from any callback in GUI1 we can get the data by doing:
gd = guidata(gcbf) % This will have myGUI2data in it.
O.k. I think I see what you were trying to do. So here is what I did. I made a GUI in guide, and set the windowstyle to modal. This GUI has one pushbutton and one editbox. Here is all of the code in the GUI (except the standard initialization code). I call it from the command line:
d = guide_modal
and it returns whatever is in the editbox. I think this is what you were trying to accomplish. Let me know:
% --- Executes just before guide_modal is made visible.
function guide_modal_OpeningFcn(hObject, eventdata, handles)
handles.output = hObject;
guidata(hObject, handles);
uiwait(handles.figure1); % stop further execution.
% --- Outputs from this function are returned to the command line.
function varargout = guide_modal_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.dat; % Set the output.
delete(handles.figure1) % delete the GUI
function edit1_Callback(hObject, eventdata, handles)
% --- Executes during object creation...
function edit1_CreateFcn(hObject, eventdata, handles)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.dat = get(handles.edit1,'string'); % Get data
guidata(handles.figure1,handles) % Save data.
uiresume(handles.figure1) % push play
Lukas
el 28 de Sept. de 2012
Matt Fig
el 28 de Sept. de 2012
So if you use code just like the above, does it work? Like I said, it works here.... Did you remember to set the windowstyle to modal in the Property inspector?
Lukas
el 1 de Oct. de 2012
Categorías
Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!