I am trying to color a box on a plot.
22 ビュー (過去 30 日間)
古いコメントを表示
採用された回答
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 件)
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!