how can solve this probleme ??? Undefined function or variable

2 views (last 30 days)
hello
how can i use the information defined in pushbutton1_Callback in the other pushbutton2_Callback without have this msg error
??? Undefined function or variable "net".
Error in ==> untitled>pushbutton2_Callback at 260
plot(net(2,:),net(3,:),'r.','MarkerSize',15);
Error in ==> gui_mainfcn at 98
feval(varargin{:});
Error in ==> untitled at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)untitled('pushbutton2_Callback',hObject,eventdata,guidata(hObject))

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Nov 2015
Edited: Geoff Hayes on 1 Nov 2015
Ali - you can easily make data available in other callbacks by using the handles structure (which is passed as the third parameter to each of your callbacks, assuming that you are using GUIDE). What you need to do is save the net data to handles in the callback for pushbutton1, and then access it from handles in the callback for pushbutton2. For example,
function pushbutton1_Callback(hObject, eventdata, handles)
% do something to get net and update the handles structure
handles.net = net;
guidata(hObject,handles); % now save the updated handles structure
Note the use of guidata which is important in saving the updated handles structure. Now, in your second callback access net as
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'net')
plot(handles.net(2,:),handles.net(3,:),'r.','MarkerSize',15);
end
And that is it. The isfield is used above to make sure that the net exists in handles before we try to access it.

Community Treasure Hunt

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

Start Hunting!