Different colors for each item in listbox of an app designer

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 Comments

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?
Hi Paolo,
I want to do the latter.
"Have a different color for every element when viewing all the elements from the list"

Sign in to comment.

More Answers (1)

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 Comments

Thank you for your reply.
I have tried using your method(s), but was unable to access the handle of the listbox
fig = gcf;
fig.Children(x)...String{1}
the above code created a new figure and returned the handle for it.
guidata too didn't work (or maybe I dont know, how to use it in Appdesigner of 2016b).
In the appdesigner, the properties of a component can be accessed by app.component.property.
I have tried
app.lb_Files.Items{end+1}= '<HTML><FONT COLOR=rgb(255,225,201) SIZE="6" FACE="ARIAL"><b><i>Test</i></b></HTML>';
app.lb_Files.ItemsData=(1:1:length(app.lb_Files.Items));
The html string appears in the variable 'Items'
but is displayed differently in the app/gui
Could it be a possibility that matlab deals with 'html strings' changed since the suggested post ?
@Pablo as well as others: any other ideas or suggestions?
Thanks!
Karthik,
Yes, as I explained in the answer the gcf depends on your application. For appdesigner, you can find all the elements with:
findall(groot)
That said, I tried to change the color in the same way as I suggested for uicontrol elements for the appdesigner. It appears that you are right, its not accepting multiple colors.
I tested for both a ListBox and a Drop Down element.
appdes = findall(groot);
%Item 5 is drop down element.
appdes(5).Items = {'<HTML><FONT COLOR="rgb(255,1,255)">v1</HTML>',...
'<HTML><FONT COLOR="blue">v2</HTML>'
and
%Item 3 is List box element.
appdes(3).Items = {'<HTML><FONT COLOR="rgb(255,1,255)">v1</HTML>',...
'<HTML><FONT COLOR="blue">v2</HTML>'
Unsuccessfully. I also tried setting multiple values for the font color directly:
%Worked fine as you probably concluded yourself.
appdes(3).FontColor = [0 1 0];
%Does not work.
appdes(3).FontColor = {[0 1 0];[1 1 0]};
It did not work. Could not find any additional information in the documentation.
I also tried creating a UIcontrol element separately and appending it to the appdesigner figure.
%appdes(2) is the figure
uicntrl = uicontrol(appdes(2),...
'Units','normalized',...
'Style','popup',...
'String',{'<HTML><FONT COLOR="rgb(255,1,255)">v1</HTML>',...
'<HTML><FONT COLOR="blue">v2</HTML>'});
And got the following error:
Error using uicontrol Functionality not supported with figures created with the uifigure function. For more information, see Graphics Support in App Designer.
So that operation is not supported.
I am really out of ideas! I did assume that since my solution worked fine for uicontrol elements, a similar approach could be used for appdesigner, however it appears I was mistaken. Perhaps contact MW directly for clarification? The documentation for dropdown and listbox do not mention multiple colors so maybe the feature is just not supported. In that case, is building your application with uicontrol elements rather than appdesigner an option?
P.S. It's Paolo :)
Hi Paolo,
Thanks for your effort man! Really appreciate it!
I shall contact MW directly. Let's see what they have got to say.
PS: Sorry for using different name, i misspelt it subconsciously!
You are welcome, my pleasure. Please let me know what the final outcome is, perhaps I can edit my answer with that information once you receive it, for it to be more visible to other users and for a comparison between uicontrol elements and appdesigner.
That's alright, not the first and not the last!
An excerpt from the tech support's email:
"Thank you for your response. I did consult with my colleagues in development regarding this. Unfortunately, as of now App Designer's ListBox and DropDown does not support having every name in the listbox with different font colors in the app designer app. I will go ahead and file an enhancement request for this. The developers might add this in a future release."
@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.
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

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!