MATLAB Answers

Animating an m-file to show a rectangle filling up

12 views (last 30 days)
Sebastian Lopez
Sebastian Lopez on 3 Mar 2020
Answered: Jakob B. Nielsen on 4 Mar 2020
I am trying to create an animation of a rectangle filling up. I already have the code to get a graph of the rectangle:
% A cylindrical tank has a capacity of 100 bottles.
% Created six rectangles within the 3.5 ft by 6 ft tank.
x1=0.5; x2=2.25; y1=1; y2=3;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
hold on;
x1=2.25; x2=4; y1=1; y2=3;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
hold on
x1=0.5; x2=2.25; y1=3; y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
hold on
x1=2.25; x2=4; y1=3; y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
hold on
x1=0.5; x2=2.25; y1=5; y2=7;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
hold on
x1=2.25; x2=4; y1=5; y2=7;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
axis([0 5 0 8]);
% Created a percent range between 0% and 100% in 10 divisions.
yline(1,'--','0%');
yline(1.6,'--','10%');
yline(2.2,'--','20%');
yline(2.8,'--','30%');
yline(3.4,'--','40%');
yline(4,'--','50%');
yline(4.6,'--','60%');
yline(5.2,'--','70%');
yline(5.8,'--','80%');
yline(6.4,'--','90%');
yline(7,'--','100%');
% Plot title and axis labels.
title('Bottle Accumulator on the x-y Plane');
xlabel('Width (ft)');
ylabel('Height (ft)');
but I don't know what functions to use to show the animation of the rectangle filling up.
What functions can I use in my m-file to create an animatioin of the rectangle filling up?
Can the function be changed to fill the rectangle until a certain point?

  0 Comments

Sign in to comment.

Answers (1)

Jakob B. Nielsen
Jakob B. Nielsen on 4 Mar 2020
I don't know what kind of animation you are looking for, but something quick and simple would be to insert a pause after every plot call. In this case, you might want to set the x and y axis limits after the first plot, so you get the full picture from the get go. I dont know if this is what you're after?
% A cylindrical tank has a capacity of 100 bottles.
% Created six rectangles within the 3.5 ft by 6 ft tank.
figure
x1=0.5; x2=2.25; y1=1; y2=3;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
xlim([0 5]);
ylim([0 8]);
pause(0.5)
hold on;
x1=2.25; x2=4; y1=1; y2=3;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
pause(0.5)
hold on
x1=0.5; x2=2.25; y1=3; y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
pause(0.5)
hold on
x1=2.25; x2=4; y1=3; y2=5;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
pause(0.5)
hold on
x1=0.5; x2=2.25; y1=5; y2=7;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
pause(0.5)
hold on
x1=2.25; x2=4; y1=5; y2=7;
x = [x1, x2, x2, x1, x1];
y = [y1, y1, y2, y2, y1];
plot(x, y, 'k');
pause(0.5)
axis([0 5 0 8]);
% Created a percent range between 0% and 100% in 10 divisions.
yline(1,'--','0%');
pause(0.5)
yline(1.6,'--','10%');
pause(0.5)
yline(2.2,'--','20%');
pause(0.5)
yline(2.8,'--','30%');
pause(0.5)
yline(3.4,'--','40%');
pause(0.5)
yline(4,'--','50%');
pause(0.5)
yline(4.6,'--','60%');
pause(0.5)
yline(5.2,'--','70%');
pause(0.5)
yline(5.8,'--','80%');
pause(0.5)
yline(6.4,'--','90%');
pause(0.5)
yline(7,'--','100%');
% Plot title and axis labels.
title('Bottle Accumulator on the x-y Plane');
xlabel('Width (ft)');
ylabel('Height (ft)');

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!