필터 지우기
필터 지우기

I am trying to color a box on a plot.

조회 수: 25 (최근 30일)
Charlie
Charlie 2024년 3월 6일
댓글: Charlie 2024년 3월 6일
i have a box with text in it that moves along a line on a plot. i need there to be background color in the box under the text. Right now I can only get it to show on top of the text.

채택된 답변

Chunru
Chunru 2024년 3월 6일
You can change the face color of the box:
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2,"FaceColor", "g");
You also need to change the order of box and text. MATLAB draw the object layer by layer. If you want to show text on top of box, you have to plot box first.
%% Header
% Written by C. Vogen
% Date Written:3/3/2024
% Modified: None
%% Variable Glossery
% t_start = 0; % seconds
% t_end = 10; % seconds
% x_final = 10; % meters
% y_final = 0; % meters
% g = 9.81 % m/s^2
% m = 150;
%
%% Code Statements
clear; clc; close all;
t_start = 0; % seconds
t_end = 10; % seconds
x_final = 10; % meters
y_final = 0; % meters
g = 9.81; % m/s^2
v_x0 = x_final/t_end; % finding initial x velocity
v_y0 = (y_final + (g*0.5*(t_end^2)))/t_end; % finding initial y velocity
m = 11;
T = zeros(m,2);
incrv = (t_end - t_start)/(m-1);
startCannon =[0,0]; endCannon = [.175,10]; % starts the cannon
Cannon = [startCannon;endCannon];
figure
plot(Cannon(:,1),Cannon(:,2),'LineWidth',5)
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
hold on
pause(1)
Color_start = [.25 .25 .25];
Color_end = [1 1 0];
Color_incv = (Color_end - Color_start)/(m-1);
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2,"FaceColor", "g");
flag_text = text(0, 0, '', 'HorizontalAlignment', 'left','VerticalAlignment','bottom', 'FontWeight', 'bold');
for i = 1:m
t = t_start +(i-1)*incrv;
x = v_x0 * t;
y = (v_y0 * t) - ((g * t^2)/2);
Color = Color_start +(i-1)*Color_incv;
pause(0.4)
if i>1
delete(ball) % removing the previous marker
delete(line) % removes the line connecting the box to the point
end
ball = plot(x,y,'o','MarkerFaceColor','r'); % plotting inside the loop
set(ball, 'XData',x, 'YData',y)
set(flag_text, 'Position', [x+0.2, 30], 'String', {['X: ', num2str(x)], ['Y: ', num2str(y)],['I: ', num2str(i)],['T: ',num2str(t)]})
set(square, 'Position', [x, 25, 2.5, 38]);
line = plot([x,x],[0,30],'b','LineWidth',2);
axis normal
title('Visual Loop')
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
grid on
end
  댓글 수: 1
Charlie
Charlie 2024년 3월 6일
It worked! Thankyou very much for helping me

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chunru
Chunru 2024년 3월 6일
Try the following
figure;
an = annotation("textbox", "Color", "r", "BackgroundColor", "g", "String", "Text Box");
for i=0:0.01:0.9
an.Position= [i, 0.5, 0.1, 0.1];
drawnow
end
  댓글 수: 4
Charlie
Charlie 2024년 3월 6일
I'll just send the whole code
%% Header
% Written by C. Vogen
% Date Written:3/3/2024
% Modified: None
%% Variable Glossery
% t_start = 0; % seconds
% t_end = 10; % seconds
% x_final = 10; % meters
% y_final = 0; % meters
% g = 9.81 % m/s^2
% m = 150;
%
%% Code Statements
clear; clc; close all;
t_start = 0; % seconds
t_end = 10; % seconds
x_final = 10; % meters
y_final = 0; % meters
g = 9.81; % m/s^2
v_x0 = x_final/t_end; % finding initial x velocity
v_y0 = (y_final + (g*0.5*(t_end^2)))/t_end; % finding initial y velocity
m = 11;
T = zeros(m,2);
incrv = (t_end - t_start)/(m-1);
startCannon =[0,0]; endCannon = [.175,10]; % starts the cannon
Cannon = [startCannon;endCannon];
figure
plot(Cannon(:,1),Cannon(:,2),'LineWidth',5)
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
hold on
pause(1)
flag_text = text(0, 0, '', 'HorizontalAlignment', 'left','VerticalAlignment','bottom', 'FontWeight', 'bold');
Color_start = [.25 .25 .25];
Color_end = [1 1 0];
Color_incv = (Color_end - Color_start)/(m-1);
square = rectangle('Position', [0, 0, 2.5, 15], 'EdgeColor','b','LineWidth',2);
for i = 1:m
t = t_start +(i-1)*incrv;
x = v_x0 * t;
y = (v_y0 * t) - ((g * t^2)/2);
Color = Color_start +(i-1)*Color_incv;
pause(0.4)
if i>1
delete(ball) % removing the previous marker
delete(line) % removes the line connecting the box to the point
end
ball = plot(x,y,'o','MarkerFaceColor','r'); % plotting inside the loop
set(ball, 'XData',x, 'YData',y)
set(flag_text, 'Position', [x+0.2, 30], 'String', {['X: ', num2str(x)], ['Y: ', num2str(y)],['I: ', num2str(i)],['T: ',num2str(t)]})
set(square, 'Position', [x, 25, 2.5, 38]);
line = plot([x,x],[0,30],'b','LineWidth',2);
axis normal
title('Visual Loop')
xlabel('x [m]');xlim([-1 13])
ylabel('y [m]');ylim([0 150])
grid on
end
Chunru
Chunru 2024년 3월 6일
See the new answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by