Set unintentionally triggers uitable callback

1 vue (au cours des 30 derniers jours)
AndreasO
AndreasO le 12 Jan 2023
Modifié(e) : Bruno Luong le 12 Jan 2023
Hello,
I want to chance the background color of an uitable element with set -> 'BackgroundColor' within the function callback or from other functions.
However, the set command appears to be triggering the table callback. At least the callback runs 2 times if i click into the table, the second time with an empty event element.
Can somoene tell me what I am doing wrong?
Here is the code for changing the BackgroundColor from within the callback:
%clear vars
clf
clear all
%random table data
table_data=rand(8,3);
%table element
h_table1 = uitable(gcf,...
'Data', table_data,...
'Units', 'normalized',...
'position', [0.2,0.2,0.6,0.6],...
'CellSelectionCallback', {@test_callback}...
);
%callback of table
function test_callback(src,event)
%read table height
table_height=height(src.Data);
%read table width
%table_width=width(src.Data);
%clicked row and column
row=event.Indices(1);
%column=event.Indices(2);
%new background color matrix
color_matrix_ci = [repmat([1, 1, 1], row-1, 1); [1, 0, 0]; repmat([1, 1, 1], table_height-row, 1)];
%set background color
set(src, 'BackgroundColor',color_matrix_ci) ;
end
  2 commentaires
AndreasO
AndreasO le 12 Jan 2023
The error is easy to reproduce if you add a breakpoint into the callback function and write into the command line of matlab:
color_matrix_ci=zeros(8,3);
set(h_table1, 'BackgroundColor',color_matrix_ci)
Then the table will chance to black, but the code stopps at the breakpoint in the callback (which should not have been triggered).
Bruno Luong
Bruno Luong le 12 Jan 2023
Modifié(e) : Bruno Luong le 12 Jan 2023
I see it too. It doesn't feel right this workflow.

Connectez-vous pour commenter.

Réponses (1)

Bruno Luong
Bruno Luong le 12 Jan 2023
I use uistyle to change the background, AFAIK it doesn't trigger callback unlike set()

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by