How to add more parameters to callback function.
显示 更早的评论
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)
采纳的回答
更多回答(1 个)
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)
类别
在 帮助中心 和 File Exchange 中查找有关 Structured Data and XML Documents 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

