How to plot using a loop on the same axis

1 次查看(过去 30 天)
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