1

So, I figured out how to call one gui from another and send back and forth information via varargin and varargout. However, right now I'm in a situation where I have two separate guis (one is not called from the other), and I believe I need some other method if I want to communicate between them.

More exactly, I'm making two GUIs that interact with Simulink. One GUI opens when the model is opened and keeps track of information. The other GUI will open when a block is double-clicked. I want to send information from this GUI to the information-tracking GUI.

So, from what I've searched, I can accomplish this either by using a Listener in the information-tracking GUI; or I can modify the variables in the information-tracking GUI directly using setappdata/getappdata or findall(0, ...).

So far my attemps haven't worked and I was wondering if I'm taking the write approach. Can someone point me in a direction? Let me know if I can provide more info!

Community
  • 1
  • 1
user1545722
  • 13
  • 1
  • 3

2 Answers2

0

I use the setappdata/getappdata method all the time for this sort of thing. Here is a general breakdown of what you do. When you create the figures give them a tag like this:

figure( ..., 'Tag', 'info_gui', ... ); % tag name is up to you
figure( ..., 'Tag', 'other_gui', ... ); % tag name is up to you

Anytime you need the handle to one or the other figures just make a call to findobj like this

info_gui_handle = findobj('tag','info_gui');
other_gui_handle = findobj('tag','other_gui');

Ok now lets store some example data in the info_gui that we will update later

info_gui_data.x = 1;
info_gui_data.y = 1;
setappdata( info_gui_handle, 'info_gui_data', info_gui_data);

Once you have your figures set up you can do things like this:

% First you get a handle to the info gui figure

info_gui_handle = findobj('tag','info_gui');

% Next you get the appdata thats stored in this figure.  In this example
% I have previously stored a struct variable called 
% 'info_gui_data' inside the appdata of the info_gui

info_gui_data = getappdata(info_gui_handle ,'info_gui_data');

% Make your changes whatever they are.  Here I am modifying variables x 
% and y that are stored in the struct info_gui_data

info_gui_data.x = 2;
info_gui_data.y = 2;

% Now that I've made changes to the local variable info_gui_data I can 
% now store it back into the info gui's appdata.

setappdata(info_gui_handle ,'info_gui_data',info_gui_data);

I like to store all of my figure appdata in one giant struct. Seems easier to keep track of but its up to you. Hope this helps :)

cholland
  • 251
  • 2
  • 8
0

You could also try using guidata and guihandles.

Suppose the handle to GUI1 is H1. In GUI1, when you want to store data that you can retrieve later, use:

guidata(H1,data)

In GUI2, when you need the data, use:

data = guidata(H1);

Alternatively, you could store your data in the Property 'User Data' of your uicontrol object. Make sure you set the Property 'Tag' to something valid (e.g. 'mybutton'). To access this from GUI2, use:

handles = guihandles(H1);
data = get(handles.mybutton,'UserData');
meicholtz
  • 76
  • 1
  • 6