Set unintentionally triggers uitable callback

1 view (last 30 days)
AndreasO
AndreasO on 12 Jan 2023
Edited: Bruno Luong on 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 Comments
AndreasO
AndreasO on 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 on 12 Jan 2023
Edited: Bruno Luong on 12 Jan 2023
I see it too. It doesn't feel right this workflow.

Sign in to comment.

Answers (1)

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

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!