Set unintentionally triggers uitable callback

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

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).
I see it too. It doesn't feel right this workflow.

Sign in to comment.

Answers (1)

I use uistyle to change the background, AFAIK it doesn't trigger callback unlike set()

Categories

Products

Release

R2021a

Asked:

on 12 Jan 2023

Edited:

on 12 Jan 2023

Community Treasure Hunt

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

Start Hunting!