Use Callback function to change string stored in it

2 visualizzazioni (ultimi 30 giorni)
I'm writing a code to create an array of pushbuttons. Everytime a button is it should change it's color, based on how many times the user has already clicked the button. I thought about making one single callbackfunction for all buttons. Because I need to check what color the button is I thought about giving a number stored in the button to the callbackfunction. In the callbackfunction itself I will change the color of the button and assign another number to the corresponding button.
That's what I've got so far, but I don't know what the general structure of the callback function should look like and if this even works.
%% Create Bimaru Cells
xorigin=45;
yorigin=410;
distance=35;
for i=1:9 %% Number of columns
for j=1:9 %% Number of rows
xposcell= xorigin+(j-1)*distance;
yposcell= yorigin-(i-1)*distance;
bimarugui(i,j)= uicontrol('unit', 'pixels',... %%allocate an array of white pushbuttons
'background', 'white',...
'Style', 'pushbutton',...
'Value', [1,i,j],... %%stores the counter variable, and the location
'visible', 'on',... in the array
'position',[xposcell,yposcell,distance distance],...
'enable','on',...
'Callback',{@cell_callback,get(bimarugui(i,j),'Value')});

Risposte (1)

Adam Danz
Adam Danz il 23 Mar 2020
Modificato: Adam Danz il 24 Mar 2020
Your idea to controll all button colors from a single callback function is a good idea but it can be simplified.
The callback function could store the order of the colors and one of its inputs would be the handle to the button whose color will be changed. All it has to do is look up the current color of the button within the matrix of colors and then assigne the following color. It would look something like this:
function changeButtonColor(buttonHandle)
% Define all colors
buttonColors = jet(10);
% Get current button color
currColor = buttonHandle.BackgroundColor;
% Look up which row of buttonColors matches current color
currColorIdx = find(ismembertol(buttonColors, currColor, 'ByRows', true));
% If a match wasn't found or if the index is the last row, assign row 0
if isempty(currColorIdx) || isequal(currColorIdx, size(buttonColors,1))
currColorIdx = 0;
end
% Assign next color to button
buttonHandle.BackgroundColor = buttonColors(cucurrColorIdx + 1);
*Not tested
To change the color of a button, you would just pass the button's handle to that function.
changeButtonColor(handles.Button1);
  15 Commenti
Guillaume
Guillaume il 27 Mar 2020
I'm confused now. Hasn't the GUI migrated to app designer? If so, there's no need for storing any handle. As I've explained the common callback to all the button will receive the button object that was pressed in its event.Source argument.
Adam Danz
Adam Danz il 27 Mar 2020
I can see why it's confusing since the dialog is jumping back and forth a bit. In this comment above, OP mentioned that they will stick with the uicontrol functions rather than appdesigner.
Also, each button is associated with surrounding buttons and when one is pressed, the color for the entire group changes. The additional input is not a handle to a single button. It's a vector of handles that are associated with the pressed button.
Depending on how the buttons are arranged, within a grid, for example, the callback function could detect which buttons are 'near' the pressed button in which case there wouldn't need to be an additional input of associated button handles.

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by