MATLAB Answers

How to automatically pass variables from one GUI to another GUI?

20 views (last 30 days)
Ak K
Ak K on 25 Jun 2019
Commented: Ak K on 28 Jun 2019
Hi, I know this question may have been answered in another MATLAB answers question, but I looked through many and didn't find one written in an easily understandable way for me. Essentially, I have two GUIs; one main one, and second GUI which is called via a pushbutton and holds 3 checkboxes whose values are converted into a matrix. This is my initialization code:
function Initialize_GUI(hObject,eventdata,handles)
handles.X = 1;
handles.Y = 1;
handles.Z = 1;
handles.KFlip = [handles.X handles.Y handles.Z];
assignin('caller','tempHandles',handles); % Import values from above into the main GUI
evalin('caller','handles.KFlip = tempHandles.KFlip;') % Set value of handles.KFlip to the initialization value
evalin('caller','guidata(hObject, handles);') % Within the main GUI, update the handles structure with the new value of KFlip
evalin('caller','clear tempHandles'); % Erase tempHandles structure from main GUI
% Update handles structure
guidata(hObject, handles);
In the main GUI, handles.KFlip was pre-initialized as [0 0 0]. Thus, when I clicked the button for opening GUI2 (all that is in that callback is the name of GUI2), I thought that it would change these values to [1 1 1] in the main GUI since the code above would be evaluated in the context of the main GUI. In addition, I thought that it would simply modify the main GUI's handles by changing the one value.
Instead, I don't even think that the lines above touch the main GUI. When I ran the code
In GUI2 to see what it thought it was, it simply yielded the UI Controls of GUI2 with the addition of the handles.KFlip value. It was neither the handles structure of the Main GUI (which holds many more fields) nor GUI2 (which should have included handles.X, handles.Y, etc.).
What should I do? Am I using evalin wrong? Is there a better way of doing this? If there is already an easy-to-understand explanation of automatically passing the variables, please simply post the link.


Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 26 Jun 2019
Ak - from which GUI is Initialize_GUI called? How are you calling this function? What makes you think that handles.Flip will be updated when it is handles.KFlip that is being assigned the array of ones?
Based on your above comments, you may be calling Initialize_GUI from the second GUI...and so the handles structure there will be for the second GUI and not not the first.
If you want to pass data between two GUIs, then take a look at which may be helpful. From the second GUI, you would use findobj to find the other GUI and then obtain it's handles structure.


Show 1 older comment
Ak K
Ak K on 28 Jun 2019
Oh I think I understand what you're saying now. You're saying that since the caller function than becomes GUI2 since the function "Initialize_GUI" was called in GUI2, I'm not accessing GUI1 at all. That makes sense, now I just need a way to do what I mentioned in the above comment.
Geoff Hayes
Geoff Hayes on 28 Jun 2019
You should be able to use the code from the link I included to find GUI1 from GUI2 and then make changes to the handles data of GUI1. So in the Initialize_GUI function (of GUI2) you could do
function Initialize_GUI(hObject,eventdata,handles)
% get the handle of Gui1
h = findobj('Tag','Gui1');
% if exists (not empty)
if ~isempty(h)
% get handles and other user-defined data associated to Gui1
gui1Handles= guidata(h);
gui1Handles.X = 1;
gui1Handles.Y = 1;
gui1Handles.Z = 1;
gui1Handles.KFlip = [gui1Handles.X gui1Handles.Y gui1Handles.Z];
% maybe you want to get some data that was saved to the Gui1 app
guidata(h, gui1Handles);
The above assumes that your first GUI is named Gui1 but you can modify the code as you see fit.

Sign in to comment.

More Answers (0)