首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在GUI MATLAB中为静态文本框中的值指定颜色

如何在GUI MATLAB中为静态文本框中的值指定颜色
EN

Stack Overflow用户
提问于 2020-12-31 13:30:12
回答 1查看 42关注 0票数 0

大家好,我想了解一下如何在GUI的静态文本框中为特定的值指定颜色。下面是我用来给静态文本框赋值的代码。但我不知道如何为该值指定颜色。例如,a=50,静态文本框的颜色变为绿色。当为a=60时,静态文本框的颜色变为黄色。提前谢谢。

代码语言:javascript
复制
set(ah,'handlevisibility','off','visible','off')

d= 500;
t= 10;

a= [num2str(d/t) 'km/h'];

set(handles.Speed,'String',a);
EN

回答 1

Stack Overflow用户

发布于 2020-12-31 23:36:17

我创建了一个函数,其中输入dt,然后它将生成文本框的背景色根据速度(d/t)变化的图形。为了将速度转换为颜色,我设置了一个color_matrix变量,它的第一列是速度,接下来的3列是指定颜色所需的红、绿和蓝值。您可以添加更多行以包含更多颜色,例如从红色到黄色再到绿色。

代码语言:javascript
复制
function ShowSpeed(d, t)
figure(1)
clf
ah = uicontrol('style', 'text', 'units', 'normalized', 'Position', [0.5, 0.5, 0.3, 0.1], 'FontSize', 12);

velocity = d / t;

a = [num2str(velocity) 'km/h'];

% first column is the velocity, 2-4 columns are the red, green, blue values respectively
color_matrix = [50, 0, 0.5, 0; ... % dark breen for 50
                60, 1, 1, .07]; % yellow for 60
background_color = interp1(color_matrix(:, 1), color_matrix(:, 2:4), velocity, 'linear', 'extrap'); 
% make sure color values are not lower than 0
background_color = max([background_color; 0, 0, 0]);
% make sure color values are not higher than 1
background_color = min([background_color; 1, 1, 1]);

set(ah,'String', a, 'BackgroundColor', background_color);

下面是速度为50的结果:

代码语言:javascript
复制
ShowSpeed(500,10)

这是速度为60的结果

代码语言:javascript
复制
ShowSpeed(600,10)

您还可以进行插值和外推,尽管您不能走得太远。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65517006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档