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

1 vue (au cours des 30 derniers jours)
Max Müller
Max Müller le 8 Août 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 commentaire
Geoff Hayes
Geoff Hayes le 12 Août 2014
Max - is the above code within the body of a function? If so, is handles one of the inputs to this function?

Connectez-vous pour commenter.

Réponses (3)

Geoff Hayes
Geoff Hayes le 13 Août 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 le 8 Août 2014
Modifié(e) : Andy L le 11 Août 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 commentaires
Andy L
Andy L le 12 Août 2014
Have you managed it yet?
Max Müller
Max Müller le 13 Août 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 ?

Connectez-vous pour commenter.


Max Müller
Max Müller le 11 Août 2014
Guys i have no idea how to do it
  1 commentaire
Andy L
Andy L le 11 Août 2014
I've revised my answer with an example.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by