Output from handles instead of globals

I have these three different functions that all relate to the GUI, one doing some stuff, the other one using the result, and the third using the result from the 2nd. I have tried to use globals for the results, but it is a mess, so i was wondering if it was possible to use handles or something like that to do it instead?
function openFile_Callback(hObject, eventdata, handles)
global im;
im = imread(myimage);
-
function analyzeImage_Callback(~, ~, ~)
global im
global dat
level = graythresh(im);
diff_im = im2bw(im,level);
area = sum(diff_im);
dat = [area];
-
function exportResults_Callback(hObject, eventdata, handles)
global dat
csvwrite('csvlist.dat',dat)

 Accepted Answer

Bjoern
Bjoern on 17 Feb 2011
i cant get nested functions to work,when i try to run my code after making it nested i get this error:
??? Error while evaluating uimenu Callback
??? Error using ==> feval Undefined function or method 'openFile_Callback' for input arguments of type 'struct'.
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> cellcounter at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)cellcounter('openFile_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uimenu Callback

6 Comments

From the names, am I right in assuming that the GUI layout was done in GUIDE? If so, the generated code probably used subfunctions... ergh.
OK, probably easiest to go to plan B: use get/setappdata:
setappdata(handles.figure1,'image',im);
then
im = getappdata(handles.figure1,'image');
you mean like:
function openFile_Callback(hObject, eventdata, handles)
im = imread(myimage);
setappdata(handles.figure1,'image',im);
function analyzeImage_Callback(hObject, eventdata, handles)
im = getappdata(handles.figure1,'image');
imshow(im);
Because i still get an error with that.
Yep, just like that. So what error do you get now?
i get the error that im is empty, "??? Undefined function or variable "im".
".
Is setappdata( the same as set(
No, setappdata() is *not* the same as set() ! Please read the documentation!
it got it working. Thanks for the help

Sign in to comment.

More Answers (1)

Matt Tearle
Matt Tearle on 17 Feb 2011
If these functions all live in the same file, the simplest way to pass data around is to use nested functions. A nested function has access to its parent's workspace.
The other way is to use getappdata/setappdata to embed the data in the figure handle.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 17 Feb 2011

Community Treasure Hunt

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

Start Hunting!