passing values between guis?

hey
i am building a gui that calls another gui.
1. i enter data into my main gui
2. then i press a pushbutton to call the sub gui
3. then i display the data got from the main gui in my sub gui.this happens upon the press of a push button.
right until this step everything seems to be working ok. i used the following code, which i got off http://blinkdagger.com/matlab/matlab-gui-how-to-easily-share-data-between-two-separate-guis/
code:
function pushbuttongetparams_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttongetparams (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mainguiFigureHandle = maingui; %stores the figure handle of main GUI here
%stores the GUI data from main GUI here
%now we can access any of the data from main GUI!!!!
mainData = guidata(mainguiFigureHandle);
%store the input text from main GUI
%into the variable maingui_input
maingui_input = get(mainguiData.primetext,'String');
%set the static text on subgui GUI to match the
%input text from maingui GUI
set(handles.primetxt2,'String',maingui_input);
my problem is that everytime i press the pushbutton on the subgui to retrieve data from the maingui, the maingui pops open too! after which i have to go back to the subgui by toggling the windows. how do i get rid of this?
i have tried using set(0,’CurrentFigure’,secondGUIname) but that does not work. is there a simple way to get rid of this problem? hopefully something that does not involve global variables or structures or even more lines of code(!) etc.!
p.s. i have gone through the other answers on the site, and none of them seem to be working for me.

 采纳的回答

I assume your main gui is called maingui? If so, then this line:
mainguiFigureHandle = maingui;
is what is creating another maingui. I would get rid of this line and alter the next to:
mainData = guidata(gcbf);
I also assume that somewhere along the way you have stored the handle to the subgui textbox in the handles structure as primetxt2...

5 个评论

i tried out what you said. i am still getting an error. something along the lines of figurehandle undefined. so i am still stuck with this problem, any help would be greatly appreciated.
Please give the exact error message, not an approximation...
And show the new code!
my new code is this:
function pushbuttongetparams_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttongetparams (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%guivalpha1FigureHandle = guivalpha1;
%close;
%guivalpha1Data = guidata(guivalpha1FigureHandle);
guivalpha1Data = guidata(gcbf);
guivalpha1_input = get(guivalpha1Data.primetext,'String');
set(handles.primetxt2,'String',guivalpha1_input);
guidata(hObject, handles)
this is the error that i was getting :
Reference to non-existent field 'primetext'.
Error in ==> gui2>pushbuttongetparams_Callback at 202
guivalpha1_input = get(guivalpha1Data.primetext,'String');
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> gui2 at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> guidemfile>@(hObject,eventdata)gui2('pushbuttongetparams_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
details:
my main gui is called--> guivalpha1
my sub gui is called--> gui2
primetext is an edit box in my guivalpha1, this is where the value is fed in .
primetxt2 is a text box in my gui2, this is the text box that extracts the value stored in guivalpha1's field primetext
You don't have a fieldname of guivalpha1Data called primetext. To look at the fields, simply take the semicolon off of this line:
guivalpha1Data = guidata(gcbf)
Then when you call the function, the structure will be printed to the command window so you can look at the fields, then go back and pick the correct name...
found a method to pass data to the sub gui! i used this example from blinkdagger -->http://blinkdagger.com/matlab/matlab-gui-using-a-sub-gui-to-manage-setting-parameters/
but thanks to you too!

请先登录,再进行评论。

更多回答(1 个)

doc setappdata %use h=0 to store the data in the base workspace

1 个评论

It's not the base workspace, but the root directory. The root directory contains all of the settings for MATLAB etc. so it can be dangerous. As long as you use fancy names for the variables (especially: underscores, numbers) it should be safe.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

产品

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by