MATLAB Answers

Matlab slider pass value to event handler

14 ビュー (過去 30 日間)
Duccio Mugnaini
Duccio Mugnaini 2014 年 7 月 9 日
回答済み: Duccio Mugnaini 2014 年 7 月 9 日
I am new of Matlab GUI and i have the following problem. I have declared a slider control and his properties, and i have added a listerner to the callback and to the PostSet event handler (i think that it is tecnically called event handler) as you can see below:
function [] = HandlerSlide()
%HANDLERSLIDE Summary of this function goes here
% Detailed explanation goes here
clf;
due = '2';
hSlider = uicontrol( ...
'Style','slider', ...
'Callback',@(s,e) disp(['hello ',num2str(due),' asdad']),...
'Position', [400 30 200 20] ... %[x,y, widht, height]
);
hListener = addlistener(hSlider,'Value','PostSet',@pippo);
end
function [] = pippo(s,e)
disp('ciao');
end
As you can see i have used parameter "due" in the Callback handler (the anonymous function). Now i would like to pass parameter to use in the "pippo" function without declare it as anonymous function. Is it possible? In other words i would like to declare "hListerner" like this:
hListener = addlistener(hSlider,'Value','PostSet',@pippo{parameter1,parameter2, etc ...});
function[] = pippo(s,e, parameter1, parameter2, etc ...)
Beside how can i use in the main the value returned by "pippo"?
thank you in advance :D

回答 (4 件)

Anthony Poulin
Anthony Poulin 2014 年 7 月 9 日
Hi,
If you want to pass parameter with your callback you can do like this:
hListener = addlistener(hSlider,'Value','PostSet',@{pippo,parameter1,parameter2, etc ...});
Then the declaration of your function is like you said:
function pippo(src,event,parameter1, parameter2, etc...)
For the second question, I'm not sure to ask correctly. Because it is a function the variable you create executing your callback are in your function workspace and be destroyed at the end of the callback execution.
But if you want to pass a value (or many) to your main workspase (the 'base' workspace), you can use the matlab function "assignin", (look also to evalin which is helpful to catch a value from a worskpace to your function worksspace)

Duccio Mugnaini
Duccio Mugnaini 2014 年 7 月 9 日
Thank you for answer, but if i write:
hListener = addlistener(hSlider,'Value','PostSet',@{pippo,var1,var2});
Matlab underline '{' '}' and give the following parse error: usage might be invalid MATLAB syntax.

Anthony Poulin
Anthony Poulin 2014 年 7 月 9 日
OK, you're right is see my mistake: I worte "@{pippo...", but it's "{@pippo..." the correct syntax.
I never used the addlistener fonction, so I don't know if it's work well with this function but I have allready do thing like this:
hSlider = uicontrol( ...
'Style','slider', ...
'Callback',{@pippo,parameter1,parameter2, etc ...},...
'Position', [400 30 200 20] ... %[x,y, widht, height]);
Is it helpfull?

Duccio Mugnaini
Duccio Mugnaini 2014 年 7 月 9 日
Yes that is helpfull to pass parameter to Callback method :D But if i use the same style to set the listener as you suggest
hListener = addlistener(hSlider,'Value','PostSet',{@pippo,var1,var2});
then the error is disappeared but when i run the function there is the following error:
Error using HandlerSlide (line 56)
No method 'addlistener' with matching signature found. Callback must be a function handle.
:(

Community Treasure Hunt

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

Start Hunting!

Translated by