nested fnc / error:I nput argument "handles" is undefined.

조회 수: 5 (최근 30일)
Max Müller
Max Müller 2014년 8월 8일
답변: Geoff Hayes 2014년 8월 13일
I have no idea what matlab is telling me.
The Error : Input argument "handles" is undefined.
The Code:
Data = findobj('type','axes');
set(Data,'buttondownfcn',@click)
function click (gcbo,evendata,handles) % here is the nested fcn
Data = gca
TitleHandle = get(Data,'title');
Title = get(TitleHandle,'string')
Names = getappdata(handles.Workspace,'Names')
AmountOfCameras = length(Names )
for i = 1:AmountOfCameras
Name = Names {i,1}
if strcmp(Name,Title) == 1
disp(i)
end
end
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2014년 8월 12일
Max - is the above code within the body of a function? If so, is handles one of the inputs to this function?

댓글을 달려면 로그인하십시오.

답변 (3개)

Geoff Hayes
Geoff Hayes 2014년 8월 13일
Max - if you wish to have access to the handles structure from within a callback that you have created (in this case, for the button down function) then consider using guidata. This will allow you to access the most recent copy of the handles data whenever you need it. The problem with trying to pass this structure as an input to your function is that it will only be a copy at the time the callback is assigned (as the button down function) and so will not have the most recent data.
In your pushbutton1_Callback do the following
function pushbutton1_Callback(hObject, eventdata, handles)
% Some Code defining a GUI, pushbuttons and callbacks etc.
handles.a = 1;
handles.b = 2;
set(gcf,'buttondownfcn',@buttonPress_product)
% save the handles data
guidata(hObject,handles);
I've added just the last line of code so that the user-defined a and b fields of handles are now saved to the structure. (Without this, these fields will not exist outside the local copy of handles.)
Now, in your buttonPress_product function, do the following
function buttonPress_product(hObject, EventData)
handles = guidata(hObject);
a = handles.a;
b = handles.b;
c = a * b;
disp(['Product is ',num2str(c)]);
handles.c = c;
% Update handles information.
guidata(hObject, handles);
The only differences with your function is that I've removed the input parameter handles, and have added the line
handles = guidata(hObject);
Which will get the most recent copy of handles including the a and b that have been defined previously.
Try out the above and see what happens!

Andy L
Andy L 2014년 8월 8일
편집: Andy L 2014년 8월 11일
You have not yet defined your 'handles' variable before passing it into your function 'click'. This is typically where you pass any data needed into the function.
Handles are a structure you define, as you would any other variable - in this instance it is the structure containing the data you need to use in the local function.
Below is an example of defining the handles for the variables a and b - to be used by a pushbutton, to display the product of two numbers. This will not work without some code to set the GUI up, but should demonstrate to you you how to define your own handles.
function [] = myGUI
% Some Code defining a GUI, pushbuttons and callbacks etc.
handles.a = 1;
handles.b = 2;
% Local Function
function buttonPress_product(hObject, EventData, handles)
% hObject - handle to the source object.
% Event Data - structure containing data/info about the event that triggered the callback.
% handles - structure defined in your code containing the information used in the function.
%
a = handles.a;
b = handles.b;
c = a * b;
disp(['Product is ',num2str(c)]);
handles.c = c;
% Update handles information.
guidata(hObject, handles);
end
end
  댓글 수: 3
Andy L
Andy L 2014년 8월 12일
Have you managed it yet?
Max Müller
Max Müller 2014년 8월 13일
I ve created a new GUI, using ur example Code like this
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% Some Code defining a GUI, pushbuttons and callbacks etc.
handles.a = 1;
handles.b = 2;
set(gcf,'buttondownfcn',@buttonPress_product)
% Local Function
function buttonPress_product(hObject, EventData, handles)
% hObject - handle to the source object.
% Event Data - structure containing data/info about the event that triggered the callback.
% handles - structure defined in your code containing the information used in the function.
%
a = handles.a;
b = handles.b;
c = a * b;
disp(['Product is ',num2str(c)]);
handles.c = c;
% Update handles information.
guidata(hObject, handles);
Now Matlab still says: Nope Input argument "handles" is undefined.
I am forced to work with v2006a. (by my supervisor). Does this makes any difference ?

댓글을 달려면 로그인하십시오.


Max Müller
Max Müller 2014년 8월 11일
Guys i have no idea how to do it

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by