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

1 Ansicht (letzte 30 Tage)
Max Müller
Max Müller am 8 Aug. 2014
Beantwortet: Geoff Hayes am 13 Aug. 2014
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 Kommentar
Geoff Hayes
Geoff Hayes am 12 Aug. 2014
Max - is the above code within the body of a function? If so, is handles one of the inputs to this function?

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Geoff Hayes
Geoff Hayes am 13 Aug. 2014
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 am 8 Aug. 2014
Bearbeitet: Andy L am 11 Aug. 2014
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 Kommentare
Andy L
Andy L am 12 Aug. 2014
Have you managed it yet?
Max Müller
Max Müller am 13 Aug. 2014
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 ?

Melden Sie sich an, um zu kommentieren.


Max Müller
Max Müller am 11 Aug. 2014
Guys i have no idea how to do it

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by