이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to change color of UITable cells in app designer if its value gets bigger than an amount?
조회 수: 68 (최근 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 (한국어)