Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

How to assign colour to a value in static text box in GUI MATLAB

A good day everyone, I'd like to get some ideas on how to assign colour for a specific value on my static text box for GUI. Below is the coding I used to assign value to the static text box. But I don't know how to assign colour for the value. For example, a=50, colour of static text box becomes green. When a=60, colour of static text box becomes yellow. Thanks in advance.

set(ah,'handlevisibility','off','visible','off')

d= 500;
t= 10;

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I created a function where the inputs d and t and then it will make the figure where the background color of the text box changes based on the velocity (d/t). To convert the velocity to a color, I set up a color_matrix variable whose first column is the velocity and the next 3 columns are the red, green, and blue values needed to specify the color. You can add more rows to have more colors involved, such as going from red to yellow to green.

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);

Here is the result with a velocity of 50:

ShowSpeed(500,10)

enter image description here

Here is the result with a velocity of 60

ShowSpeed(600,10)

enter image description here

You can also interpolate and extrapolate, although you can't go too far.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share

1.2m questions

2.1m answers

5 comments

56.5k users

...