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)

 採用された回答

Bjoern
Bjoern 2011 年 2 月 17 日

0 投票

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 件のコメント

Matt Tearle
Matt Tearle 2011 年 2 月 17 日
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');
Bjoern
Bjoern 2011 年 2 月 17 日
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.
Matt Tearle
Matt Tearle 2011 年 2 月 18 日
Yep, just like that. So what error do you get now?
Bjoern
Bjoern 2011 年 2 月 21 日
i get the error that im is empty, "??? Undefined function or variable "im".
".
Is setappdata( the same as set(
Walter Roberson
Walter Roberson 2011 年 2 月 21 日
No, setappdata() is *not* the same as set() ! Please read the documentation!
Bjoern
Bjoern 2011 年 2 月 21 日
it got it working. Thanks for the help

サインインしてコメントする。

その他の回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 2 月 17 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2011 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by