在 MATLAB 中更改部分字符串的颜色

问题描述 投票:0回答:2

假设我们有这些代码:

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
的颜色吗?

谢谢。

matlab user-interface text matlab-guide
2个回答
2
投票

要为字符串的一部分着色,请使用 cprintf 函数。

可以参考这个:http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window


0
投票

我最近发现需要这样做,以下是如何将上面提到的 tex 格式与 sprintf 的附加选项结合起来:

cstr = strrep(sprintf('foo (bar) = %d',41),'bar','{\color{red}bar}');
© www.soinside.com 2019 - 2024. All rights reserved.