假设我们有这些代码:
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
应为red
,其他单词应为默认值(black
)。我知道我可以为 String_2
创建一个单独的静态文本并手动更改它的颜色,但无论如何要更改上述结构中 String_2
的颜色吗?
谢谢。
要为字符串的一部分着色,请使用 cprintf 函数。
可以参考这个:http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window
我最近发现需要这样做,以下是如何将上面提到的 tex 格式与 sprintf 的附加选项结合起来:
cstr = strrep(sprintf('foo (bar) = %d',41),'bar','{\color{red}bar}');