MATLAB Answers

how can i change the Color of the Row (uitable) when if==0? Die farbe der reihe im IUtable soll sich öndern wenn if ==0

37 views (last 30 days)
Torsion27 on 10 Apr 2018
Commented: Robert U on 19 May 2020
function pushbutton2_Callback(hObject, eventdata, handles)
global pushbutton
[num,txt,raw]= xlsread(uigetfile ({'.xlsx'}))
anzahl_kriterien = size([raw],1)
kriterium = cell(anzahl_kriterien);
for i=1:anzahl_kriterien
kriterium{i,1} = raw(i)
tabledata = [num2cell(true(length(raw),1)),raw];
set(handles.uitable3, 'data',tabledata)
function uitable3_CreateFcn(hObject, eventdata, handles)
hObject.ColumnFormat = {'logical',[]};
hObject.ColumnEditable = logical([1 0]);
function uitable3_CellEditCallback(hObject, eventdata, handles)
for j = 1:length(tabledata) %für zeile 1 bis ende
for i=2
if (tabledata{j,1}==0)
set(uitablehandles,'BackgroundColor',[1 0 0]);
elseif (tabledata{j,1}==1)
set(uitablehandles,'BackgroundColor',[0 1 0]);
  1 Comment
Himanshu Verma
Himanshu Verma on 19 May 2020
Hi Torsion27, did you find the solution to this problem? I'm using 2019a and want a backward compatible code in which I can change the foreground/background color of specific row. I tried using HTML but uifigure doesn't recognise html code. I can't use 'addStyle' or 'uistyle' as well. Please suggest a solution if you found any.

Sign in to comment.

Answers (1)

Robert U
Robert U on 19 May 2020
Hi Torsion27,
using uifigure and uitable there is an easy-to-apply solution on your data. WIthout going through your code I recite the example of Matlab documentation. The addStyle option "target" does the trick, finally. You would have to adapt to your code.
tdata = readtable('tsunamis.xlsx');
vars = {'Year','Month','Day','Hour','MaxHeight','Cause','EarthquakeMagnitude'};
tdata = tdata(1:100,vars);
fig = uifigure('Position',[500 500 750 350]);
uit = uitable(fig);
uit.Position = [20 20 710 310];
uit.Data = tdata;
uit.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
% the following line is changed compared to original example to account for
% your requirement to highlight the row instead of cell.
Adaption would be to assign "row" and "col" in your corresponding "if"-clause (maybe applying logical indexing).
Kind regards,
Robert U
Robert U on 19 May 2020
Hi Himanshu Verma,
I did not test the following feature for all matlab versions between 2016b and 2019b. I used it in 2016b before I changed to 2019b (which would allow for "addStyle").
You can run a switch-case-command on version('-release') to ensure functionality in different matlab versions.
Kind regards,

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!