You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to change color of UITable cells in app designer if its value gets bigger than an amount?
106 views (last 30 days)
Show older comments
Hi All
I need to show the output of my code on an nxn table so at any time, not all but any of these values might exceed certain limits. and I want to change that cell color if its value has exceeded that value. How do I do that ?
and if you wanna add an example file, I use version 2017b. thank you very much
Accepted Answer
Ameer Hamza
on 23 Apr 2020
This example explain how you can color cells in a uitable based on conditions: https://www.mathworks.com/help/releases/R2020a/matlab/ref/uitable.html#mw_17e7164d-b6a6-40dd-a69e-fb422ee43794. If there is some issue to implement this method then you can ask in the comment.
32 Comments
Ameer Hamza
on 23 Apr 2020
I am not sure, I don't have R2017b installed. Can you try to change the cell color of uitable in App designer using this method in R2017b?
farzad
on 23 Apr 2020
I have one problem, that is : it first defines a figure with position ,and then it takes th uitable of that figure
but in my app desginer, I don't need to indicate where the UItable is. since it's already put in the automatic part of the code. I am not sure how to apply this
fig = uifigure('Position',[500 500 750 350]);
uit = uitable(fig);
Ameer Hamza
on 23 Apr 2020
You don't need to run these lines. App designer already creates the figure and uitable. Just try the following lines, and replace 'uit' with app.UITable
uit.Data = tdata;
uit.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
addStyle(uit,s,'cell',[row,col]);
farzad
on 23 Apr 2020
well, I got this error
Undefined function or variable 'tdata'.
Error in appAll/ExecuteButtonPushed (line 1474)
uit.Data = tdata;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 309)
Error while evaluating Button PrivateButtonPushedFcn.
farzad
on 23 Apr 2020
Thank you, yes actually I was using cell data.
I did this in my code, first converted the cell data to table :
app.UITable.Data = num2cell(matrix_result);
tdata= cell2table(app.UITable.Data);
uit.Data=tdata;
uit.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
addStyle(uit,s,'cell',[row,col]);
but I got this error :
Undefined function 'uistyle' for input arguments of type 'char'.
Error in appAll/ExecuteButtonPushed (line 1480)
s = uistyle('BackgroundColor','yellow');
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 309)
Error while evaluating Button PrivateButtonPushedFcn.
farzad
on 23 Apr 2020
and I checked uit :
uit =
struct with fields:
Data: [6×6 table]
and they seem to be numbers
Ameer Hamza
on 23 Apr 2020
Farzed: uistyle was introduced in R2019b: https://www.mathworks.com/help/releases/R2020a/matlab/ref/uistyle.html#mw_2c6c5d14-ea97-404e-bf27-ffcbc6973d4a_seealso. For previous releases, it might not be possible to format the background colors of table cells.
farzad
on 23 Apr 2020
Thank you very much, just one question , how do I specify to chane only one specific cell of the uitable if its value goes bellow or beyound a limit ?
Ameer Hamza
on 23 Apr 2020
Data property of uitable are just like a normal table. You can modify it using indexing
app.UITable.Data{2,3} = 5; % change value if 2nd row and 3rd column
farzad
on 23 Apr 2020
thank you very much, so if changin color is not possible on UITable for version 2017b, it won't be possible to change the edit field background color neither right ?
Ameer Hamza
on 23 Apr 2020
Documentation of R2017b shows that editfield have a property named background color: https://www.mathworks.com/help/releases/R2017b/matlab/ref/matlab.ui.control.editfield-properties.html#property_d119e1196668. It should be possible to change it in R2017b.
farzad
on 23 Apr 2020
thank youvery much. Can I use the EditField to show the calculation output and not allow the user write in it ?
Ameer Hamza
on 23 Apr 2020
Yes. You can set the editible property to false. You can find this property in component browser -> Interactivity.
farzad
on 24 Apr 2020
Ok I tried it on 2019b.
app.UITable.Data = num2cell(matrix_result);
tdata= cell2table(app.UITable.Data);
uit.Data=tdata;
uit.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
addStyle(uit,s,'cell',[row,col]);
now with what I had written I get the error
Undefined function 'addStyle' for input arguments of type 'matlab.ui.style.Style'.
farzad
on 24 Apr 2020
dear Ameer, I think I can't load the whole code or I should modify it a lot. but the matrix_result is a 6x6 numerical matrix that In my code I converted it to table. but it seems that all lines work , till the last command that gives error. shall you please kindly tell me how to check and verify where the problem is ?
I also only need to highlight few of the cells
farzad
on 24 Apr 2020
I printed the output and I have these :
uit =
struct with fields:
Data: [6×6 table]
styleIndices =
6×6 logical array
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
row =
0×1 empty double column vector
col =
0×1 empty double column vector
s =
Style with properties:
BackgroundColor: [1 1 0]
Show all properties
Undefined function 'addStyle' for input arguments of type 'matlab.ui.style.Style'.
Error in appAllR2019b/ExecuteButtonPushed (line 1482)
addStyle(uit,s,'cell',[row,col])
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
Ameer Hamza
on 24 Apr 2020
Documentation shows that addStyle was added in R2019b, so I am not sure why you would get this error. Are you using R2019b, or R2019a?
farzad
on 24 Apr 2020
It's existent
C:\Program Files\Polyspace\R2019b\toolbox\matlab\uicomponents\uicomponents\+matlab\+ui\+control\@Table\addStyle.m
Ameer Hamza
on 24 Apr 2020
Can you show what does the function signature of this file looks like. In R2020a, it is
function addStyle(obj, styleObject, varargin)
which clearly show that it can accept style object.
farzad
on 24 Apr 2020
It's the same :
function addStyle(obj, styleObject, varargin)
so maybe the problem is the way I have change the uit in my code ?
I think I have sth wrong in the first 3 lines maybe ?
app.UITable.Data = num2cell(matrix_result);
tdata= cell2table(app.UITable.Data);
uit.Data=tdata;
uit.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
addStyle(uit,s,'cell',[row,col]);
Ameer Hamza
on 24 Apr 2020
Try using the following code. app.UITable is the handle of UITable in app designer.
tdata = readtable('tsunamis.xlsx');
vars = {'Year','Month','Day','Hour','MaxHeight','Cause','EarthquakeMagnitude'};
tdata = tdata(1:100,vars);
app.UITable.Data = tdata;
app.UITable.RowName = 'numbered';
styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','yellow');
addStyle(app.UITable,s,'cell',[row,col]);
farzad
on 24 Apr 2020
now I get this error a little bit before in the line ismissing, I don't have NAN objects, how do I change it to say , if any value bigger than 0.1 ?
Error using matlab.internal.math.ismissingKernel/arraySwitch (line 79)
First argument must be numeric, logical, datetime, duration, calendarDuration, string, categorical, char, cellstr, table, or timetable.
Error in matlab.internal.math.ismissingKernel (line 16)
IA = arraySwitch(A,false);
Error in ismissing (line 70)
IA = matlab.internal.math.ismissingKernel(A);
Himanshu Verma
on 19 May 2020
I'm using 2019a and working on a code that should be backward compatible with 2016 version onwards. So, I can't use 'addStyle' and 'uistyle'. I tried changing the cell colors using 'html' but the html code is not interpreted in 'uifigure'. How can I then set the foreground/background color of each row to different colors? Please help.
Ameer Hamza
on 19 May 2020
I don't think there is a documented way to do that before R2019b. If the HTML trick didn't work, then most likely not an easy thing.
farzad
on 27 Jul 2020
Hello Ameer. May you please check this question ? https://www.mathworks.com/matlabcentral/answers/570907-how-to-dynamically-define-limits-forplot-from-user-input-in-app-designer
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)