Create push buttons with a For loop and provide individual callbacks within the For loop.

1 view (last 30 days)
Stefan Fretschner
Stefan Fretschner on 19 Jan 2022
Commented: Stefan Fretschner on 19 Jan 2022
Hello!
Here are two minimal examples.
The first one runs. Unfortunately, a larger (maybe also variable) number of buttons is necessary.
When I try to solve this with a for-loop, I always get error messages.
Is it at all possible to implement an individual callback for each individual button in a loop?
Thx for Help :-)
%%
% create a varible WaveDatei = [22 33 55] bevore starting the function
function minimalExamplePushButton
f = figure('units','normalized',...
'position', [0.1 0.2 0.2 0.2]);
button1 = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[0.1 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback','display(WaveDatei(1))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
button2 = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[0.4 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback','display(WaveDatei(2))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
button3 = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[0.7 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback','display(WaveDatei(3))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
end
%%
% create a varible WaveDatei = [22 33 55] bevore starting the function
function minimalExamplePushButton2
f = figure('units','normalized',...
'position', [0.1 0.2 0.2 0.2]);
for cc = 1:3
button(cc) = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[(0.2*cc) 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback','display(WaveDatei(cc))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
end
end
  1 Comment
Stephen23
Stephen23 on 19 Jan 2022
"When I try to solve this with a for-loop, I always get error messages."
Do not supply the callback function as text. That approach is fragile, and the documentation clearly states "Defining a callback as a character vector is not recommended."
Use a much more efficient and more robust function handle instead.
"Is it at all possible to implement an individual callback for each individual button in a loop?"
Of course (you can create any number of function handles in a cell array), but it would really be much easier to create just one parameterized function for all of those buttons:

Sign in to comment.

Accepted Answer

_
_ on 19 Jan 2022
When you specify a callback as a character vector, the callback executes in the MATLAB workspace (reference here), which may or may not have the variable 'cc' (and if it does have a variable called 'cc' the value of cc will not be related to the cc used in the function where the callback is specified). Instead of using a character vector, you can use a function handle to specify the callback, with additional input arguments as needed. Like this:
% create a varible WaveDatei = [22 33 55] bevore starting the function
function minimalExamplePushButton2
f = figure('units','normalized',...
'position', [0.1 0.2 0.2 0.2]);
for cc = 1:3
button(cc) = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[(0.2*cc) 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback',{@cb_button,cc}, ...'display(WaveDatei(cc))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
end
end
function cb_button(src,evt,button_idx)
display(button_idx);
end
Of course, if you want to access the variable WaveDatei from within the callback, WaveDatei has to be accessible to your GUI. There are various ways to do that (the handles structure - see guidata; nested functions). Here's how you can do it with a nested callback function:
function minimalExamplePushButton2()
% Create figure and pushbuttons:
f = figure('units','normalized',...
'position', [0.1 0.2 0.2 0.2]);
for cc = 1:3
button(cc) = uicontrol('Parent',f,...
'Style','PushButton', ...
'units','normalized',...
'Position',[(0.2*cc) 0.7 0.125 0.125], ...
'FontName','Arial', ...
'FontSize',14, ...
'String','Play', ...
'Callback',{@cb_button,cc}, ...'display(WaveDatei(cc))',...
'BackgroundColor',hsv2rgb([0.33 0.5 0.66]));
end
% Create some data the GUI needs:
WaveDatei = [22 33 55];
% Callback is nested so it can access variables in
% minimalExamplePushButton2()'s workspace:
function cb_button(src,evt,button_idx)
display(WaveDatei(button_idx));
end
end

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!