问题
I have two gui's one is main gui and other is sub gui. In opening function of main gui i used open('subgui.fig'); to open sub gui. Main consists of 5 edit box and one pushbutton. After pressing pushbutton the data in those 5 edit boxes should be passed to sub gui and main gui should close. Please any one help me to do this.
回答1:
Let us take a simple case of one editbox and one pusbutton in main GUI
and one editbox in sub GUI
that will get value from the editbox in main GUI. One can easily extend this to as many editboxes as needed. The basic medium of data storage and retrieval would be a global structure data1
.
For the sake of understanding the codes, let us take the following assumptions -
- Main GUI is named as
main_gui.m
and thus has an associatedmain_gui.fig
from GUIDE. Main GUI's figure has the tagmain_gui_figure
. - Sub GUI is named as
sub_gui.m
and thus has an associatedsub_gui.fig
from GUIDE.
Edits to be made in main_gui.m
Inside editbox's callback, add this -
global data1;
%%// Field in data1 to store the string in editbox from main GUI
data1.main_gui.edit1val = get(hObject,'String');
Inside the pushbutton's callback, add this right before it's return -
global data1;
sub_gui;
delete(handles.main_gui_figure);
Edits to be made in sub_gui.m
Inside sub_gui_OpeningFcn, add this -
global data1;
set(handles.edit1,'String',data1.main_gui.edit1val);%%// Tag of editbox in sub-gui is edit1
Hope this works out for you! Let us know!
回答2:
There are probably more than one ways to achieve this. But one of the approaches is to define a function that takes two input arguments: 1) handles to the destination figure and 2) whatever data from the source figure.
The following psuedo code doesn't necessarily run in MATLAB, but it gives the basic idea:
function takeAction(uihdls, data)
set(0, 'CurrentFigure', uihdls.fig); % uihdls.fig is the handle of the destination figure.
set(gcf, 'CurrentAxes', uihdls.aexs1); % axes1 is inside fig
plot(data.x, data.y); % Do some plotting
set(uihdls.editBox, 'String', data.string); % Modify some property of a control inside fig.
key_Callback(uihdls.fig, data.keyData); % Call a callback function of the destination figure
return
This function can be called by the source figure whenever it is ready to do so.
回答3:
A bit more work - but I think it's worth it.
I usually use the MVC pattern for that. Practically it means to write a controller object that will pass the messages through to the required fields.
来源:https://stackoverflow.com/questions/22525622/pass-data-between-guis-matlab