MATLAB Answers

Getappdata and setappdata

73 views (last 30 days)
Agata
Agata on 5 Dec 2011
Edited: John Kelly on 12 Jun 2014
Hi, I have a problem with getappdata and setappdata. I was wrote the matlab help but I don't understand everything. Do you have any example code with this metods?

Accepted Answer

Chandra Kurniawan
Chandra Kurniawan on 5 Dec 2011
Hello,
I will give you sample code for this topic.
I have a .fig file (figure) that consists of 2 axes (axes1 and axes2) and 2 pushbutton (pushbutton1 and pushbutton2).
Then, here the m-file code
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
I = imread('cameraman.tif');
imagesc(I, 'parent', handles.axes1);
setappdata(handles.pushbutton1,'img',I);
function pushbutton2_Callback(hObject, eventdata, handles)
J = getappdata(handles.pushbutton1,'img');
bw = im2bw(J);
imagesc(bw, 'parent', handles.axes2);
Watch at pushbutton1_callback. Fisrt I read image with command imread and display it to the axes1.
Then after finished, I store the variable I to the handle : 'pushbutton1' as name : 'img' with command :
setappdata(handles.pushbutton1,'img',I);
Then watch at pushbutton2_callback
I used command
J = getappdata(handles.pushbutton1,'img');
to get the the last value which I stored at handle 'pushbutton1' which name = 'img'.
And then I successfully create a binary image with input parameter 'J'

Community Treasure Hunt

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

Start Hunting!