Matlab slider pass value to event handler
4 views (last 30 days)
Show older comments
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
0 Comments
Answers (4)
Anthony Poulin
on 9 Jul 2014
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)
0 Comments
Anthony Poulin
on 9 Jul 2014
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?
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!