Change Color of each individual String in a Listbox ?

조회 수: 46 (최근 30일)
Max Müller
Max Müller 2014년 9월 1일
댓글: Adam 2019년 11월 1일
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 ?

채택된 답변

Adam
Adam 2014년 9월 2일
편집: Adam 2014년 9월 2일
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.
  댓글 수: 9
Max Müller
Max Müller 2014년 9월 3일
Thanks, so much guys :D
Guillaume
Guillaume 2014년 9월 3일
The colours and names are embedded in the strings, so you could parse them:
liststrings = get(hlistbox, 'string');
colnames = regexp(liststrings, 'color="(.*)">(.*)</FONT', 'tokens', 'once');
data = cell2struct(reshape([colnames{:}], 2, []), {'color', 'name'});

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

추가 답변 (4개)

Adam
Adam 2014년 9월 1일
Slightly modified example of code Yair Altman posted many years ago:
figure; uicontrol('Style','list', 'String', ...
{'<HTML><FONT color="red">Red</FONT></HTML>', ...
'<HTML><FONT color="green">Green</FONT></HTML>', ...
'<HTML><FONT color="blue">Blue</FONT></HTML>'} );
  댓글 수: 7
Keqin Xu
Keqin Xu 2019년 10월 31일
Trouble is, when there are "<" in the string, the "<" are all missing in the listbox!
Any ideas to fix it? Thanks!

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


Max Müller
Max Müller 2014년 9월 1일
but i very new to programing..... can u pls explain me, how I cant set the ListboxInput and color each Word with a special Color (RGB)
  댓글 수: 4
Adam
Adam 2014년 9월 1일
dec2hex( [r g b] )
should help convert between the two, although you have to scoop up the 3 rows of the result into a single string to put into the html string.
Guillaume
Guillaume 2014년 9월 2일
That would be:
reshape(dec2hex([r g b], 2)',1, 6)

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


Max Müller
Max Müller 2014년 9월 2일
it sucks
  댓글 수: 1
Image Analyst
Image Analyst 2014년 9월 2일
Max, this is not an official "Answer" to your original question. If anything, it should have been a Comment to some other response.

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


Max Müller
Max Müller 2014년 9월 2일
I just need an interactive way to show some text with a unique color and give this thing an on click callback.
  댓글 수: 2
Adam
Adam 2014년 9월 2일
Please don't keep adding new questions as answers, it is confusing.
You can define the colour of independent static text objects easily enough (or indeed any independent objects like that if they are predefined).
If you want to have different coloured backgrounds or text in a listbox or uitable or similar though then I'm not aware of any alternative to using html and/or the underlying java UI programming as discussed in previous answers.
If you can explain which element of the suggested solution you are having a problem with we can assist further on that.
Max Müller
Max Müller 2014년 9월 2일
Imagine i have a structure called Data.
Data.name
Data.Color
are its Input. Now I want to write the names into a Listbox and give the String the Color form Data.Color.

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by