"Undefined function or variable "o1Bound" " whereas that variable have been used in another function and it works, how can I fix this error ?

1 view (last 30 days)
I got the code from here http://www.comp.hkbu.edu.hk/~vincent/resTool.htm (fingerprint verification) and I implement this in my project
% --- Executes on button press in btn_direction.
function btn_direction_Callback(hObject, eventdata, handles)
image1 = handles.image1;
guidata(hObject,handles);
axes(handles.axes2);
[o1Bound,o1Area]=direction(image1,16);
guidata(hObject,handles);
% --- Executes on button press in btn_ROI.
function btn_ROI_Callback(hObject, eventdata, handles)
image1 = handles.image1;
guidata(hObject,handles);
axes(handles.axes2);
[o2,o1Bound,o1Area]=drawROI(image1,o1Bound,o1Area);
guidata(hObject,handles);
the error is on this line > [o2,o1Bound,o1Area]=drawROI(image1,o1Bound,o1Area);

Accepted Answer

Geoff Hayes
Geoff Hayes on 29 Oct 2015
Faza - the scope for the variables o1Bound and 01Area is local to the btn_direction_Callback function only and so are not available to any other function. What you can do instead is to save these variables to the handles structure and access them through it in the other callback. For example,
function btn_direction_Callback(hObject, eventdata, handles)
if isfield(handles,'image1')
axes(handles.axes2);
[o1Bound,o1Area]=direction(handles.image1,16);
handles.o1Bound = o1Bound; % add the two fields to handles
handles.o1Area = o1Area;
guidata(hObject,handles); % save the updated handles structure
end
Now, in your second callback you would do the following
function btn_ROI_Callback(hObject, eventdata, handles)
if isfield(handles,'image1') && isfield(handles,'o1Bound') && isfield(handles,'o1Area)
axes(handles.axes2);
[o2,o1Bound,o1Area]=drawROI(handles.image1,handles.o1Bound,handles.o1Area);
% do you need to save the output of drawROI??
end
Note how the isfield function to check to see if the variable that we want to access is a field within the handles structure. We use guidata only when we want to save the updated handles structure to the hObject (parent) figure.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!