Imread passing in callbacks

Hi everybody,
I'm working with GUI and I have a function that gets image's path and saves it to the variable in workspace:
[filename, pathname] = uigetfile({'*.jpg';'*.png';'*.gif';'*.*'},'File Selector');
assignin('base','sciezka',[pathname filename]);
and I want to read that variable 'sciezka' from another function which is located in another pushbutton
m = imread(sciezka);
and the error says
Undefined function or variable 'sciezka'.
Error in CMa (line 14)
m = imread(sciezka);

Answers (2)

Dawid - if you are writing a variable to the base workspace using one callback function, then you will have to implement some code (in the other callback) that reads that variable from the base workspace. Simply referring to it by name is not sufficient, and so the error that you are observing makes sense.
Try to think of an alternative to this. Are you using GUIDE to design your GUI? If so, then you could make use of the handles structure: you would update it with the path and file information in one callback, and then reference it in another. Something like
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile({'*.jpg';'*.png';'*.gif';'*.*'},'File Selector');
% update the handles structure and save
handles.sciezka = [pathname filename];
guidata(hObject,handles);
We use guidata to save the updated handles structure so that other callback can access the most recent version. Now, in your other callback, you could do
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'sciezka')
m = imread(handles.sciezka);
end
In the above, we check to make sure that sciezka is a field of handles before trying to access it.
If you are not using GUIDE and are (instead) creating your GUI programmatically, then consider nesting your functions (callbacks) within the main body of your script/function that you run to create the GUI. One benefit of nested functions is that they have access to the local variables that have been declared in the body of your script. So one callback can set this local variable sciezka, while the other can read from it.
Try either of the above scenarios and see what happens!

4 Comments

Ok I see, but what if my callback funtion uses a function outside the GUI ?
function startButtonID_Callback(hObject, eventdata, handles)
% hObject handle to startButtonID (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[m_retPerctString, m_retRecgString] = CMainWin();
set(findobj('Tag','myText'),'String',m_retRecgString)
set(findobj('Tag','text9'),'String',m_retPerctString)
if isfield(handles,'sciezka')
m = imread(handles.sciezka);
end
How to pass that handle structure to the function CMainWin() ?
Modify the signature of the function CMainWin so that it can accept input parameters.
Call:
[m_retPerctString, m_retRecgString] = CMainWin(handles);
Definition:
function [m_retPerctString, m_retRecgString] = CMainWin(handles)
If you change handles inside CMainWin you'll need to return handles also, or else call guidata().
Thank you all for helping. I get it now.

Sign in to comment.

Image Analyst
Image Analyst on 19 Jan 2015
Good MATLAB programmers usually hate it when people use assignin() and evalin(). Better ways are shown in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 18 Jan 2015

Commented:

on 19 Jan 2015

Community Treasure Hunt

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

Start Hunting!