Loop for plotting severa variables

조회 수: 9 (최근 30일)
Felipe Rios
Felipe Rios 2019년 9월 11일
댓글: Felipe Rios 2019년 9월 12일
I am trying to plot Depth against Salinity, Temperature and Oxygen collected by a CTD profiler at several stations. Lenght of data is variable depending of water depth at eact station.
My current code is as follow:
subplot(2,3,1)
hold on;
plot(T1,D1, 'b -');
plot(T2,D2, 'r --');
title('Perfil Temperatura (°C)')
yticks(0:5:100);
set(gca,'xaxisLocation','top');
set(gca,'YDir','reverse');
ylabel('m');
legend('Est.1','Est.2','Location','south' )
subplot(2,3,2)
hold on
plot(S1,D1, 'b -');
plot(S2,D2, 'r --');
title ('Perfil Salinidad (PSU)')
yticks(0:5:100);
set(gca,'xaxisLocation','top');
set(gca,'YDir','reverse');
ylabel('m');
legend('Est.1','Est.2', 'Location','south' )
subplot(2,3,3)
hold on
plot(DO1,D1, 'b -');
plot(DO2,D2, 'r --');
title ('Perfil Oxígeno (mg/L)')
yticks(0:5:100);
set(gca,'xaxisLocation','top');
set(gca,'YDir','reverse');
ylabel('m');
legend('Est.1','Est.2', 'Location','south' )
How can I use a loop to come up with a shorter code??

채택된 답변

Walter Roberson
Walter Roberson 2019년 9월 11일
X1 = {T1, S1, D01};
X2 = {T2, S2, D02};
Y1 = D1;
Y2 = D2;
titles = {'Perfil Temperatura (°C)', 'Perfil Salinidad (PSU)', 'Perfil Oxígeno (mg/L)'};
for K = 1 : 3
ax = subplot(2, 3, K);
hold(ax, 'on');
plot(ax, X1{K}, Y1, 'b -');
plot(ax, X2{K}, Y2, 'r --');
title(ax, titles{K});
yticks(0:5:100:
set(ax, 'xaxisLocation', 'top');
set(ax, 'YDir', 'reverse');
ylabel(ax, 'm');
legend(ax, 'Est.1','Est.2', 'Location','south' );
end
  댓글 수: 1
Felipe Rios
Felipe Rios 2019년 9월 12일
Works like a charm! Many thanks Walter.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by