Set unintentionally triggers uitable callback

조회 수: 1 (최근 30일)
AndreasO
AndreasO 2023년 1월 12일
편집: Bruno Luong 2023년 1월 12일
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
AndreasO
AndreasO 2023년 1월 12일
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 2023년 1월 12일
편집: Bruno Luong 2023년 1월 12일
I see it too. It doesn't feel right this workflow.

댓글을 달려면 로그인하십시오.

답변 (1개)

Bruno Luong
Bruno Luong 2023년 1월 12일
I use uistyle to change the background, AFAIK it doesn't trigger callback unlike set()

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by