필터 지우기
필터 지우기

How to plot using a loop on the same axis

조회 수: 1 (최근 30일)
Tebogo Makobe
Tebogo Makobe 2021년 8월 14일
댓글: Tebogo Makobe 2021년 8월 14일
Would like to plot without having to hard code every single line up to 20+ customers(20 x 1 structure). Had an attempt at a for loop, the error message states that the number of element on the right are not same as on the left
%Graphical representation
t = 0:0.1:310; % Time vector for plots (m)
[position1,speed1,Duration1] = calcFallForPerson(customer(1),4200,0.1,1500);
[position2,speed2,Duration2] = calcFallForPerson(customer(2),4200,0.1,1500);
[position3,speed3,Duration3] = calcFallForPerson(customer(3),4200,0.1,1500);
[position4,speed4,Duration4] = calcFallForPerson(customer(4),4200,0.1,1500);
figure(1)
plot(t,position1,t,position2,t,position3,t,position4)
title('Posiion')
xlabel('Time(s)')
ylabel('Postion(m)')
xlim([0 320])
ylim([0 4500])
figure(2)
plot(t,speed1,t,speed2,t,speed3,t,speed4)
title('Velocity')
xlabel('Time(s)')
ylabel('Velocity(m/s)')
xlim([0 320])
ylim([0.0 60.0])
xlim([0 320])
ylim([0.0 60.0])

채택된 답변

Wan Ji
Wan Ji 2021년 8월 14일
Is the time array t the same size as position1,speed1 for each loop with different customers? If so, then following loop will help you!
t = 0:0.1:310; % Time vector for plots (m)
number_of_customer = 20; % change as you want
[position,speed,Duration] = arrayfun(@(i)acalcFallForPerson(customer(i),4200,0.1,1500),...
1:1:number_of_customer, 'uniform', false);
figure(1)
arrayfun(@(i)plot(t,position{i}),1:1:number_of_customer);
title('Posiion')
xlabel('Time(s)')
ylabel('Postion(m)')
xlim([0 320])
ylim([0 4500])
figure(2)
arrayfun(@(i)plot(t,speed{i}),1:1:number_of_customer);
title('Velocity')
xlabel('Time(s)')
ylabel('Velocity(m/s)')
xlim([0 320])
ylim([0.0 60.0])
xlim([0 320])
ylim([0.0 60.0])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by