- 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 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
Jan
2018 年 8 月 4 日
編集済み: Jan
2018 年 8 月 4 日
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
...
参考
カテゴリ
Help Center および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!