Different colors for each item in listbox of an app designer

27 ビュー (過去 30 日間)
Karthik  Vemireddy
Karthik Vemireddy 2018 年 6 月 5 日
回答済み: Jasmine Poppick 2023 年 3 月 17 日
Hi,
In my app, i have a list box with multiple items and i would like to display each item in a different color.
The list and the assigned colors have to change dynamically.
Is there a way to do it?
Thanks!
  2 件のコメント
Paolo
Paolo 2018 年 6 月 5 日
Do you want to change the color of the text/background according to what value is selected, or do you want to have a different color for every element when viewing all the elements from the list?
Karthik  Vemireddy
Karthik Vemireddy 2018 年 6 月 5 日
Hi Paolo,
I want to do the latter.
"Have a different color for every element when viewing all the elements from the list"

サインインしてコメントする。

採用された回答

Jasmine Poppick
Jasmine Poppick 2023 年 3 月 17 日
Starting in R2023a, you can do this in App Designer and with uilistbox by using addStyle.

その他の回答 (1 件)

Paolo
Paolo 2018 年 6 月 5 日
編集済み: Paolo 2018 年 6 月 5 日
It is possible to change the color of a uicontrol popup element as explained here. You can specify colors ("red","blue","green") or include a rgb triplet.
An example:
uicntrl = uicontrol(panel,...
'Units','normalized',...
'Style','popup',...
'String',{'<HTML><FONT COLOR="rgb(255,1,255)">v1</HTML>',...
'<HTML><FONT COLOR="blue">v2</HTML>'});
The two elements in the popup dropdown list will have different colors. As you indicated that you wish to do this programmatically, a possible solution is the following:
1. Find element.
You can find the element you are interested in by using the handle to the current figure and determining which child you need. You will need to substitute (x) and its children accordingly to your gcf.
fig = gcf;
fig.Children(x)...String{1}
Alternatively, you can use guidata, adding the uicontrol element directly to the structure which enables you to access said element more easily across multiple functions.
2. Change string value.
fig.Children(x)...String{1} =
<HTML><FONT COLOR="rgb(255,1,255)">v1</HTML>
Let's say the current value is rgb(255,1,255), and you wish to change the rgb triplet to rgb(5,12,25).
You can achieve this by using regexprep as explained by Walter here.
With
n1 = 5;
n2 = 12;
n3 = 25;
The replacement expression:
rep = strcat(num2str(n1),'$1',num2str(n2),'$2',num2str(n3));
Replace rgb triplet with new values:
fig.Children(x)...String{1} = regexprep(fig.Children(x)...String{1}, '\d+(\D+)\d+(\D+)\d+', rep);
The color of the text of the element will then change automatically to the new rgb triplet:
rgb(5,12,25)
  7 件のコメント
Paolo
Paolo 2018 年 6 月 13 日
@Karthik
Thank you for letting me know. Perhaps I can include this information in my answer? You can accept it for it to be visible to other users. I believe the proposed solution of doing it with uicontrol elements manually and/or programmatically can still be of interest for other users.
Owen Stadlwieser
Owen Stadlwieser 2020 年 5 月 8 日
What you can do is insert an app.HTML element link it to an html file and create a list with different colour elements in that html file

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by