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

11 ビュー (過去 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 Center および File ExchangeContour Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by