필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Brandon burns
Brandon burns 2021년 6월 21일
답변: Divija Aleti 2021년 6월 24일
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)'

답변 (1개)

Divija Aleti
Divija Aleti 2021년 6월 24일
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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by