Azzera filtri
Azzera filtri

Callback on a Grid of Buttons Only Works on Last Button Created

4 visualizzazioni (ultimi 30 giorni)
Hello MATLAB reader! I am making a GUI and I have used a for loop to create 9 buttons.
screensize = get(groot,'ScreenSize') ; % g root provides a way to globally configure the default apperance at the root graphics level
halfscreensizecentre = [(screensize(1,[3,4])/4),(screensize(1,[3 4]))/2] ; % creates a value for position and size equal to half
% First 2 numbers in matrix correspond to position, later two to size. % the screen size in the centre of the screen.
figurecolour = [230/255 230/255 230/255] ; % Controls colour in R G B form between 0 and 1
mainmenu = figure('Name','Applied Thermofluids Calculator by Philip Schneider','Position',halfscreensizecentre,'Color',...
figurecolour,'MenuBar','none','NumberTitle','off') ; % displays the figure
buttoncolor = [0 64/255 115/255] ; % MATLAB blue
buttonwidth = .25 ; % buttonwidth = 1/(number of buttons per row + 1)
buttonheight = .2 ; % buttonheight = 1/(numberofbuttons per column + 1)
rowspace = buttonheight / 5; % 4 rows so 5 spaces % Distance between rows and columns
columnspace = buttonwidth / 4; % 3 columns so 4 spaces
loopnumber = 1 ;
for numberofcolumns = 1:3 % This for loop plots the bottom 9 buttons efficiently
loopnumber2 = 1 ;
rowdistance = (rowspace*loopnumber) + (-1 + loopnumber)*buttonheight ;
for numberofrows = 1:3
columndistance = (columnspace*loopnumber2) + (-1 + loopnumber2)*buttonwidth ;
grid = uicontrol("Style","pushbutton","Units","normalized","Position",[columndistance,rowdistance,buttonwidth,buttonheight],...
"BackgroundColor",buttoncolor,'UserData','testing') ;
loopnumber2 = loopnumber2 + 1 ;
end
loopnumber = loopnumber + 1 ;
end
for numberofcolumns = 1:3 % This for loop (CURRENTLY DOES NOT) creates functionality for the bottom 9 buttons efficiently
for numberofrows = 1:3
set(grid,'Callback',{@selectedbutton,grid})
end
end
function selectedbutton(object,~,grid)
object.Position
object.UserData
grid
end
The problem is that only when I press the last button created (top right) I get a response. My question is how can I fix this so that all the buttons will respond when pressed?
Thank you reader very much.

Risposta accettata

Aditya
Aditya il 4 Giu 2024
Hi Philip,
The issue arises because the callback is being set outside of the loop that creates the buttons, and it's only applied to the last button (grid) created due to the way the variable is being overwritten in each iteration. To ensure that each button has its callback function, you should move the callback setting inside the loop where you create the buttons. Here's the minimal change needed:
Instead of having a separate loop for setting the callbacks, integrate the callback assignment inside your button creation loop like so:
for numberofcolumns = 1:3
loopnumber2 = 1;
rowdistance = (rowspace*loopnumber) + (-1 + loopnumber)*buttonheight;
for numberofrows = 1:3
columndistance = (columnspace*loopnumber2) + (-1 + loopnumber2)*buttonwidth;
grid = uicontrol("Style","pushbutton","Units","normalized","Position",[columndistance,rowdistance,buttonwidth,buttonheight],...
"BackgroundColor",buttoncolor,'UserData','testing');
% Set the callback here, within the same loop
set(grid,'Callback',{@selectedbutton,grid});
loopnumber2 = loopnumber2 + 1;
end
loopnumber = loopnumber + 1;
end
This ensures that each button is assigned its callback as it is created, allowing all buttons to respond when pressed.
Hope this helps!

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by