Minimalist
Minimalist

Reputation: 975

MATLAB: Blanked Plots when Looping on a Multidimensional Array

My goal output is to have four plots displaying (time, dist_a), (time, speed_a), (time, dist_b), and (time, speed_b) when looping through a multidimensional array. However, I am displaying only 2 blanked plots.

Here is my code:

time = rand(10, 1)
dist_a = rand(10,1)
dist_b = rand(10,1)
speed_a = rand(10,1)
speed_b = rand(10,1)

dist = cat(2, dist_a, dist_b);
speed = cat(2, speed_a, speed_b);

for k = 1:2
figure;
plot(time, dist(k));
plot(time, speed(k));
end

Upvotes: 1

Views: 31

Answers (1)

BenI
BenI

Reputation: 160

Your problems were two-fold. Firstly, you were only plotting a single point as opposed to a vector, changing dist(k) to dist(:,k) for example fixes this. Secondly, if you want four figures with a loop that executes twice, you need to include another figure command before the second plot. The following should do what you asked for, I also added in some formatting to make the plots looks nicer

for k = 1:2
    figure
    plot(time, dist(:,k),'o','LineWidth',2);
    xlabel('time')
    ylabel('distance')
    box on
    grid on

    figure
    plot(time, speed(:,k),'o','LineWidth',2);
    xlabel('time')
    ylabel('speed')
    box on
    grid on
end

which gives:

enter image description here

Upvotes: 1

Related Questions