MATLAB: Transfering data from GUI to main file

guideMATLABmatlab guimultiple gui's

I have a GUI, call it GUI1, with two pushbuttons, the first pushbutton executes a series of calculations which are unimportant to this question, suffice to say the calculations require user input data to function. The second pushbutton in the original GUI opens a second GUI, call it GUI2, where users are prompted to input data that will be used in the calculations mentioned earlier. I need to make it so that data entered in GUI2, can be accessed for the calculations. It may be helpful to know that the file which creates the first GUI, also houses the code for the calculations, I do not call a seperate file, but have just written the calculations in, to activate on pressing of the first pushbutton. How can I make it so that the information enterred in the second GUI can be accessed in the calculations?

Best Answer

  1. Assign a unique tag to GUI2. This can be done by opening GUI2 in GUIDE, right click on the GUI background, select 'property inspector', and then assign a unique string in the "Tag" property.
  2. From GUI1, find the handle to GUI2 using: h = findobj(0, 'tag', 'UNIQUE_TAG'); where "UNIQUE_TAG" is the tag you assigned.
  3. Now get the handle to all of GUI2 components by using: handlesGUI2 = guidata(h);
  4. Now you have access to GUI2 from GUI1.
% Summary of above
% This goes in GUI1
h = findobj(0, 'tag', 'UNIQUE_TAG');
handlesGUI2 = guidata(h);
You should also write a conditional that throws an error when 'h' is empty (meaning that GUI2 could not be found) or if numel(h)>1 (meaning your tag was not unique enough or you have >1 instances of GUI2 open).