現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to change color of UITable cells in app designer if its value gets bigger than an amount?
40 ビュー (過去 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 で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)