Different color for Listbox items

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

답변 (1개)

Jan
Jan 2018년 8월 4일
편집: Jan 2018년 8월 4일

1 개 추천

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
...

댓글 수: 1

Pi Height
Pi Height 2018년 8월 4일
Thanks for the link and hint. I already fixed that. ;) I reduced the code and the question to the minimum regarding to the listbox colour isssue. The link really helped me. Sorry for not stumble upon this before.
Do you have an idea why a cell array is instead of a string not working?
Thanks for your help!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

질문:

2018년 8월 4일

댓글:

2018년 8월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by