Twinkal
Twinkal

Reputation: 424

How to pass information between GUI callbacks

I want to pass queryname which is a string and resembles full path of an image to another file do_demo_2.

function query_browse_Callback(hObject, eventdata, handles)
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.tif'},'Select Query Image');
queryname=[pathname filename];

function retrieve_Callback(hObject, eventdata, handles)
do_demo_2;

how should i modify this and what would be my 1st line in do_demo_2 file??

Upvotes: 1

Views: 1037

Answers (1)

Jonas
Jonas

Reputation: 74940

In order to pass data around in a GUI, it is most convenient to store the data in the handles-structure. Choose either version 1 or version 2 (they are not inter-compatible).

function query_browse_Callback(hObject, eventdata, handles)
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.tif'},'Select Query Image');
queryname=[pathname filename];

%# store queryname, version 1
handles.queryname = queryname;
guidata(hObject,handles);
%# store queryname, version 2
setappdata(handles.YOURGUINAME,'queryname',queryname)


function retrieve_Callback(hObject, eventdata, handles)

%# retrieve queryname, version 1
queryname = handles.queryname;
%# retrieve queryname, version 2
queryname = getappdata(handles.YOURGUINAME,'queryname');

Make sure that in the Opening_Fcn of your GUI, you initialize the stored information:

%# version 1
handles.queryname = '';
guidata(hObject,handles);

%# version 2
setappdata(handles.YOURGUINAME,'queryname','');

This way, you can check in retrieve_callback whether queryname is empty and tell the user to browse for a filename first.

If you then want to pass the contents of queryname to a different function, you pass it as an input argument: do_demo_2(queryname). Make sure the function accepts inputs.

Upvotes: 1

Related Questions