MATLAB: Change Color of each individual String in a Listbox

colorhandleslistboxstring

Hey Guys,
I want to give each Word in my List box an specific color. My mentor said: its not possible. Now I ask here: is it possible to chose the Color of Listbox text with the RGB system ?

Best Answer

Data(1).name = 'red';
Data(1).Color = [255 0 0];
Data(2).name = 'green';
Data(2).Color = [0 255 0];
Data(3).name = 'blue';
Data(3).Color = [0 0 255];
pre = '<HTML><FONT color="';
post = '</FONT></HTML>';
listboxStr = cell(numel( Data ),1);
for i = 1:numel( Data )
str = [pre rgb2Hex( Data(i).Color ) '">' Data(i).name post];
listboxStr{i} = str;
end
figure; hListBox = uicontrol('Style','list', 'Position', [20 20 100 100], 'String', listboxStr );
That should give you an example of what you want with the following function defined based on Guillame's input above :
function hexStr = rgb2Hex( rgbColour )
hexStr = reshape( dec2hex( rgbColour, 2 )',1, 6);
Obviously if you have a pre-existing listbox from Guide you can just do the usual:
set( handles.listbox1, 'String', listboxStr )
instead of creating a new listbox.
Related Question