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

22 visualizzazioni (ultimi 30 giorni)
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
evalin('caller','handles')
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.

Risposta accettata

Geoff Hayes
Geoff Hayes il 26 Giu 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 https://www.mathworks.com/matlabcentral/answers/146215-pass-data-between-gui-s which may be helpful. From the second GUI, you would use findobj to find the other GUI and then obtain it's handles structure.
  4 Commenti
Geoff Hayes
Geoff Hayes il 28 Giu 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);
end
The above assumes that your first GUI is named Gui1 but you can modify the code as you see fit.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by