MATLAB Answers

For loop to plot mutiple x and y arrays on the same plot

2 views (last 30 days)
Brandon burns
Brandon burns on 21 Jun 2021
Answered: Divija Aleti on 24 Jun 2021
Hi I have imported a numeric matrix(percentIMPS100mw) with several different variables in distinct columns. I wrote a for loop trying to plot 2 specific variables against each other and then select the next pair of variables to be plotted against each other. However with my current for loop only the x-variable is being selected each time and plotted against the initial y-variable. I included my loop below. Thanks for any help.
%% Nyquist plot
figure;
hold on
for h= 4:6:size(percentIMPS100mw,2)
for i= 5:6:size(percentIMPS100mw,2)
end
plot(percentIMPS100mw(:,h),percentIMPS100mw(:,i));
end
legend(LegendCellD,'location','northwest','NumColumns',3)
title('Nyquist Plot Device with 100 mW DC')
xlabel('Real Photocurrent(A)')
ylabel('Imaginary Photocurrent(A)'

Answers (1)

Divija Aleti
Divija Aleti on 24 Jun 2021
Hi Brandon,
From my understanding, you are first trying to plot two specific variables against each other and then two different variables against each other and so on. For this, you can store the column numbers that you wish to plot as two separate vectors, x-vector and y-vector, and then use the plot function inside a for loop. Note that the length of x and y vectors must be the same. Have a look at the code below:
figure
x = 4:6:size(percentIMPS100mw,2);
y = 5:6:size(percentIMPS100mw,2);
for i = 1:length(x)
x_column = x(i);
y_column = y(i);
plot(percentIMPS100mw(:,x_column),percentIMPS100mw(:,y_column));
hold on
end
legend(LegendCellD,'location','northwest','NumColumns',3)
title('Nyquist Plot Device with 100 mW DC')
xlabel('Real Photocurrent(A)')
ylabel('Imaginary Photocurrent(A)')
This code plots the 4th and 5th columns against each other, then 10th and 11th columns and so on.
In case you want to plot all possible pairs of columns against each other (Eg: 4th with 5th, 4th with 11th,.....,10th with 5th, 10th with 11th,...etc.), simply use the plot and hold on functions inside both the for loops in your original code.
Hope this helps!
Regards,
Divija

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by