How can I create for loop on contourf and save plots somewhere?

조회 수: 6 (최근 30일)
Behrooz Daneshian
Behrooz Daneshian 2023년 2월 7일
답변: Les Beckham 2023년 2월 7일
Hi all,
I need to create a for loop in my code to make multiple contour plots. I use the following code to plot just 1 contour map based on column 3 of "data"(data(:,3)). What I want to do is to plot multiple contour maps based on column 3, 4, 5, 6, and 7 of "data" and save all of them with a specific label somewhere in my computer. Label for the first plot should be "the probability of frost depth exceedance of 1foot", for the second plot "the probability of frost depth exceedance of 2 feet", and so on. (The last plot's label : "The probability of frost depth exceedance of 5 feet).
Can anyone help me with this?
data = cell2mat(POFDE);
I = scatteredInterpolant(data(:,[1 2]),data(:,3));
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
contourf(lon,lat,I(lat,lon),'ShowText','on')
colorbar

답변 (2개)

Fifteen12
Fifteen12 2023년 2월 7일
편집: Fifteen12 2023년 2월 7일
for i = 1:width(data)
I = scatteredInterpolant(data(:,[1 2]),data(:,i));
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
fig = contourf(lon,lat,I(lat,lon),'ShowText','on')
colorbar
label = sprintf('the probability of frost depth exceedance of %d feet.png', i);
saveas(fig, label)
end
  댓글 수: 1
Fifteen12
Fifteen12 2023년 2월 7일
편집: Fifteen12 2023년 2월 7일
Note that this doesn't change the first label from 'feet' to 'foot'

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


Les Beckham
Les Beckham 2023년 2월 7일
I'm not sure I believe this data since we are seeing probablities of > 1 in some cases (perhaps an interpolation/extrapolation artifact). However, here is how you could do what your are asking (if I understand correctly):
load('POFDE.mat');
data = cell2mat(POFDE);
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
for i = 1:(size(data, 2)-2)
figure
I = scatteredInterpolant(data(:,[1 2]), data(:,i+2));
contourf(lon, lat, I(lat,lon), 'ShowText', 'on');
colorbar
title(sprintf('the probability of frost depth exceedance of %d feet', i))
exportgraphics(gca, sprintf('FrostPlot_%d_feet.png', i))
end
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by