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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by