Callback on a Grid of Buttons Only Works on Last Button Created
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!