- https://www.mathworks.com/matlabcentral/answers/153064-change-color-of-each-individual-string-in-a-listbox
- https://undocumentedmatlab.com/blog/listbox-layout-customization
Different color for Listbox items
3 views (last 30 days)
Show older comments
Hey guys, I would like to set different colours of items in a listbox.
The code works for strings like 'a' but it doesn't work for a cell array like this:
data.Properties = {'a';'b';'c';'d';'e'};
I would like to have three cell arrays in a listbox like 'data.Properties'. Each of the cell arrays like 'data.Properties' should have a different colour in this listbox.
Why is a cell array not working and a string is? What do I have to change to get it done with the cell array?
function Test_UiControl_Listbox_small()
%%Test Function
close all
data = createData();
gui = createInterface();
function data = createData()
%%Data from Excel
data.Properties = {'a';'b';'c';'d';'e'};
%Data(1).name = data.Properties; % this is not working but why?
data.Data(1).name = 'a';
data.Data(1).Color = [255 0 0];
data.Data(2).name = 'b';
data.Data(2).Color = [0 255 0];
data.Data(3).name = 'c';
data.Data(3).Color = [0 0 255];
end
function gui = createInterface()
%%Window figure and Panel
gui.Window = figure('Units','normalized','Outerposition',[0 0 1 1],...
'Name','Test ', ...
'NumberTitle', 'off','MenuBar', 'none');
gui.panel_T = uipanel('Units','pixels','Title','Test2',...
'FontSize',10,'Position',[400 200 1100 350]);
pre = '<HTML><FONT color="';
post = '</FONT></HTML>';
listboxStr = cell(numel( data.Data ),1);
for i = 1:numel( data.Data )
str = [pre rgb2Hex( data.Data(i).Color ) '">' data.Data(i).name post];
listboxStr{i} = str;
end
gui.hListBox = uicontrol('parent',gui.panel_T,'Style','list',...
'Position', [20 20 100 100], 'String', listboxStr );
function hexStr = rgb2Hex( rgbColour )
hexStr = reshape( dec2hex( rgbColour, 2 )',1, 6);
end
end
end
0 Comments
Answers (1)
Jan
on 4 Aug 2018
Edited: Jan
on 4 Aug 2018
I recommend to take the time and ask an internet search engine for : "Matlab listbox color". You will get links like:
By the way:
switch gui.checkbox{k}
case gui.checkbox{1}
...
This is working but at least confusingly obfuscated. This would be much smarter:
if k == 1
...
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!