Eghbal
Eghbal

Reputation: 3783

Change color of a part of string in MATLAB

Suppose that we have these codes :

String_1 = 'hi';
String_2 = 'You';
set(handles.text,'String',[String_1',Who are ' String_2 '?']); %% handles.text refers to a static text in GUI

String_2 should be red in sentence and other words should be default (black). I know that I can create a separate static text for String_2 and change the color of it manually but Is anyway to change the color of String_2 in above structure?

Thanks.

Upvotes: 0

Views: 1724

Answers (2)

J B
J B

Reputation: 440

I recently found a need for this, and here's a quick example combining the tex formatting noted above with sprintf in a one-liner:

cstr = strrep(sprintf('foo (bar) = %d',41),'bar','{\color{red}bar}');

Use regexprep for more versatile substitution

Upvotes: 1

lakshmen
lakshmen

Reputation: 29064

To color part of the string, you cprintf function.

You can reference to this: http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window

Upvotes: 2

Related Questions