How to change color of UITable cells in app designer if its value gets bigger than an amount?
57 次查看(过去 30 天)
显示 更早的评论
farzad
2020-4-22
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
采纳的回答
Ameer Hamza
2020-4-23
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 个评论
Ameer Hamza
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
and I checked uit :
uit =
struct with fields:
Data: [6×6 table]
and they seem to be numbers
Ameer Hamza
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
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
2020-4-23
thank youvery much. Can I use the EditField to show the calculation output and not allow the user write in it ?
Ameer Hamza
2020-4-23
Yes. You can set the editible property to false. You can find this property in component browser -> Interactivity.
farzad
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
It's existent
C:\Program Files\Polyspace\R2019b\toolbox\matlab\uicomponents\uicomponents\+matlab\+ui\+control\@Table\addStyle.m
Ameer Hamza
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-4-24
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
2020-5-19
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
2020-5-19
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
2020-7-27
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)