How to add more parameters to callback function.
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jim Teifla
el 3 de Abr. de 2019
Comentada: Adam Danz
el 6 de Oct. de 2022
I want to add the new parameter 'a' to myfunction(obj,event_obj,a). I dont understant why there are no parameters at calling while the function has arguments.
How to call the function with 'a' parameter also?
Here is the code that is working without my new parameter:
function button_exponential_fit_Callback(hObject, eventdata, handles)
...
set(dcm, 'updatefcn', @myfunction)
function output_txt = myfunction(obj,event_obj)
pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)], ['Y: ',num2str(pos(2),4)]};
%disp(a)
0 comentarios
Respuesta aceptada
Adam Danz
el 3 de Abr. de 2019
The first two inputs for callback functions are fixed. You can add additional inputs by doing the following.
function button_exponential_fit_Callback(hObject, eventdata, handles)
...
set(dcm, 'updatefcn', {@myfunction, a})
% ________________ In curly brackets, add additional inputs
function output_txt = myfunction(obj,event_obj,a)
% ___ starting with input #3
end
4 comentarios
Sanders A.
el 5 de Oct. de 2022
Editada: Sanders A.
el 5 de Oct. de 2022
Changing it to the following per https://www.mathworks.com/help/matlab/matlab_oop/listener-callback-functions.html (I think there is a missing ')' at end of first code segment of section: Additional Arguments for Callback Function)
o.connection = udpport("datagram",'LocalHost',o.ServerName, 'EnablePortSharing', true);
configureCallback(o.connection,"datagram",1,@(srv,evnt)receiver(srv,evnt,o))
with
function receiver(h, ~, o)
worked for the correct transfering of the data, but now the issue is that none of the receiver callbacks are processsed (while in the correct manner!) until the end of program, which kind of ruins the point.
Pausing the program in the receiver function and then clicking "show all properties" for o.connection gives the following:
IPAddressVersion: "IPV4"
LocalHost: "192.168.56.1"
LocalPort: 64416
NumDatagramsAvailable: 11
ByteOrder: "little-endian"
Timeout: 10
EnablePortSharing: 1
EnableBroadcast: 0
EnableMulticast: 0
EnableMulticastLoopback: 1
MulticastGroup: ""
DatagramsAvailableFcnMode: "datagram"
DatagramsAvailableFcnCount: 1
DatagramsAvailableFcn: @(srv,evnt)receiver(srv,evnt,o)
OutputDatagramSize: 512
NumDatagramsWritten: 0
ErrorOccurredFcn: []
UserData: []
Of note are "NumDatagramsAvailable" being 11 while "DatagramsAvailableFcnCount" is set to 1. Given the latter property, I would have expected the receiver function to have run each time NumDatagramsAvailable >= DatagramsAvailableFcnCount, but given this is my first foray into udp, I must be mistaken. Might you be able to help me with this new issue?
Thank you very much!!!
Edit to add: The "client" is created as such following the original code, if it means anything. Does it need to have special options?
o.connection = udpport("datagram",'LocalHost',o.ServerName);
It was originally
o.connection = udp(o.ServerName, o.ServerPort);
fopen(o.connection);
Adam Danz
el 6 de Oct. de 2022
I'm afraid my experience with this is limited and without being able to troubleshoot I am limited to the documentation and reading the code.
Your callback should trigger whenever a new datagram is available to be read. Could it be that all 11 available diagrams are already available before the callback function is defined?
If that doesn't help getting you closer, you can contact tech support for help. Feel free to include the URL of this thread or at least copy its contents to show what you've tried.
Más respuestas (1)
Steven Lord
el 5 de Oct. de 2022
I know the cell array approach @Adam Danz suggested for passing additional parameters into a callback function works for callbacks of Handle Graphics objects, but I'm not certain it will work for callbacks of UDP objects. I'd probably use the Adapter pattern with an anonymous function. Since I'm not familiar with UDP I'm going to use a graphical example as well but I expect the technique will work with UDP.
The fsurf function requires the function handle you provide it as input to accept two arguments, x and y.
fsurf(@(x, y) x.^2-y.^2)
But if I had a function with three inputs:
f = @(x, y, a) x.^2+a*y.^2;
I could still use this in fsurf. I would just need an adapter that allows fsurf to call the function with two inputs and that calls f with three inputs.
figure
adaptedF = @(x, y) f(x, y, -1);
fsurf(adaptedF)
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

