How to change color of each individual string in a app.listbox?

조회 수: 6 (최근 30일)
kevin eyre
kevin eyre 2021년 3월 9일
댓글: JKMSMKJ 2025년 6월 10일
I can use the following code,which is useful in GUI:
a = {'<HTML><FONT color="red">Red</FONT></HTML>', ...
'<HTML><FONT color="green">Green</FONT></HTML>', ...
'<HTML><FONT color="blue">Blue</FONT></HTML>'};
figure; uicontrol('Style','list','Position',[100 100 100 100],'String', ...
a);
But it is not work in Appdesigner:
app.ListBox.Items = {'<HTML><FONT color="red">Red</FONT></HTML>'};
So is it possible in Appdesigner?

답변 (1개)

Akanksha
Akanksha 2025년 6월 10일
Hey kevin eyre,
App Designer uses web-based UI components (based on JavaScript and HTML rendering in a different way), and these components do not support HTML tags in the Items property. So, colored or styled text using HTML is not rendered.
Following is the app designer Compatible Code Using UITable with Colors that can help with the query :
functionstartupFcn(app)
% Sample data
data={'Red';'Green';'Blue'};
% Set data to UITable
app.UITable.Data=table(data,'VariableNames',{'Color'});
% Apply colors using uistyle
redStyle=uistyle('BackgroundColor',[10.80.8]);
greenStyle=uistyle('BackgroundColor',[0.810.8]);
blueStyle=uistyle('BackgroundColor',[0.80.81]);
% Apply styles to each row
addStyle(app.UITable,redStyle,'row',1);
addStyle(app.UITable,greenStyle,'row',2);
addStyle(app.UITable,blueStyle,'row',3);
end
Here’s the screenshot attached using the above code -
PFA the links for further reference
Hope this helps!
  댓글 수: 1
JKMSMKJ
JKMSMKJ 2025년 6월 10일
Hi Kevin & Akanksha,
uistyle can be used directly for uilistbox (since R2023a), so mirroring what worked for figure/GUIDE (which no longer does since R2025a), we can do the following:
a={'<p style="color:red;">Red</p>',...
'<p style="color:green;">Green</p>',...
'<p style="color:blue;">Blue</p>'};
app.listbox=uilistbox(uifigure,Position=[100 100 100 100],Items=a); %Parent is uifigure!
addStyle(app.listbox,uistyle("Interpreter","html"));
Hope this works for your use case as expected!

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

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by