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

1 次查看(过去 30 天)
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 个评论
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 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by