MATLAB: Image file transfer from one GUI to another using setappdata and getappdata

digital image processingimage processingMATLABmatlab gui

Hi, I have two matlab GUIs. First I load one image into the GUI1. Then I want to import that image from GUI1 to GUI2 for processing. After the processing I want to import that processed image from GUI2 to GUI1. How can I do that by using setappdata and getappdata. Thnak U

Best Answer

Rupam - there are probably several different ways to do this, so here is one. Let's assume that you have two GUIs named Gui1 and Gui2 and that they are distinct GUIs (i.e. you are not running two instances of the same one). In the Property Inspector for Gui1, set the HandleVisibility property to on, and the Tag property to Gui1. This will allow us to search for the GUI given its (unique) tag. Do the same for Gui2 (except name the tag Gui2). Save both.
Now suppose that in Gui2 you want access to some of the data in Gui1. What you require, is the handle to the latter. In a (say) push button callback of Gui2, you can add the following code
function pushbutton1_Callback(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
g1data = guidata(h);
% maybe you want to set the text in Gui2 with that from Gui1
set(handles.text1,'String',get(g1data.edit1,'String'));
% maybe you want to get some data that was saved to the Gui1 app
x = getappdata(h,'x');
end
The above example tests out two ways in which we can get data from the other GUI - use either guidata which gives us the handles to all widgets of Gui1 AND any user-defined data that had been saved in Gui1 to the handles object as
function pushbutton3_Callback(hObject, eventdata, handles)
% in some Gui1 callback, we create the following xData field in the handles

% structure

handles.xData = -2*pi:0.0001:2*pi;
% now we save the data

guidata(hObject,handles);
We then access this data as shown previously.
The other way is to use the setappdata and getappdata pairings. Using the previous callback for Gui1, we can save the app data as
function pushbutton3_Callback(hObject, eventdata, handles)
% in some Gui1 callback, we create the following xData field in the handles
% structure
handles.xData = -2*pi:0.0001:2*pi;
% now we save the data
guidata(hObject,handles);
setappdata(handles.Gui1,'x',[1:53]);
and then retrieve it with getappdata.
NOTE how we use must use the handles.Gui1 to save/set the data. And that is it. Try the above and see what happens!