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

20 views (last 30 days)
Torsion27
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)
end
tabledata = [num2cell(true(length(raw),1)),raw];
set(handles.uitable3, 'data',tabledata)
setappdata(handles.uitable3,'RawTableDat',raw)
function uitable3_CreateFcn(hObject, eventdata, handles)
hObject.ColumnFormat = {'logical',[]};
hObject.ColumnEditable = logical([1 0]);
function uitable3_CellEditCallback(hObject, eventdata, handles)
tabledata=get(handles.uitable3,'Data');
spalte1=tabledata(:,1)
zeilenanzahl=length(tabledata);
for j = 1:length(tabledata) %für zeile 1 bis ende
for i=2
if (tabledata{j,1}==0)
uitablehandles=findobj(handles.uitable3);
set(uitablehandles,'BackgroundColor',[1 0 0]);
elseif (tabledata{j,1}==1)
uitablehandles=findobj(handles.uitable3);
set(uitablehandles,'BackgroundColor',[0 1 0]);
end
end
end

  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.
addStyle(uit,s,'row',row);
Adaption would be to assign "row" and "col" in your corresponding "if"-clause (maybe applying logical indexing).
Kind regards,
Robert

  2 Comments

Himanshu Verma
Himanshu Verma on 19 May 2020
Hi Robert, I'm also looking for the solution. I have read the documentation on 'addStyle' and 'uistyle', but I'm currently using 2019a and I need the code to be backward compatible (atleast till version 2016). So, I can't use both of these functions. And also, the html code does not get interpreted in uifigure.
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,
Robert

Sign in to comment.