Converting Colour RGB into a string with padding and gaps

3 views (last 30 days)
Hello, I am accessing the color of lineplots and want to output them to a text area
b=ax.Children;
n=numel(b);
ReportMessage(app,'******* Graphics Objects [RGB] (1=newest) *******');
for i=1:n
T=b(i).Type; %Text or line?
C=b(i).Color %Get line colour
Lg=b(i).DisplayName;
end
These colours "C" are doubles in the form
C =
0 0.4470 0.7410
when i want to create a string from them, if I use this in the loop:
ReportMessage(app,[num2str(i,'%02d'),': ',T,', color: ',num2str(C,'%.2f'),' Lgd: ',Lg]);
I get:
01: line, color: 0.000.450.74 Lgd: 30.0 (^{o}C), 20s Delay
02: line, color: 1.000.540.00 Lgd: 27.5 (^{o}C), 20s Delay
03: line, color: 0.250.800.54 Lgd: 25.0 (^{o}C), 20s Delay
04: line, color: 0.830.140.14 Lgd: 22.5 (^{o}C), 20s Delay
where the colour RGB have no spaces between each component.
How can I get Color, so the 1st line would become (Note I need gaps between the components RGB)
01: line, color: 0.000 0.447 0.741 Lgd: 30.0 (^{o}C), 20s Delay
T
his is my ReportMessage Function
function ReportMessage(app,msg)
currString=get(app.MessagesTextArea,'Value');
%currString=[{char(msg)};currString]; %add to top of message box
currString=[currString; {char(msg)}]; %add to bottom of message box
app.MessagesTextArea.Value=currString;
drawnow;
scroll(app.MessagesTextArea,'bottom');
end

Accepted Answer

Image Analyst
Image Analyst on 18 Jun 2024
Use sprintf to make up a custom string with the info you want, then use text to place that string somewhere on your GUI.
  1 Comment
Jason
Jason on 18 Jun 2024
Thankyou I.A, this worked
formatSpec = 'Color: %.2f %.2f %.2f'; A1=C(1), A2=C(2); A3=C(3);
str = sprintf(formatSpec,A1,A2,A3)

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!