Effacer les filtres
Effacer les filtres

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

12 vues (au cours des 30 derniers jours)
Behrooz Daneshian
Behrooz Daneshian le 7 Fév 2023
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

Réponses (2)

Fifteen12
Fifteen12 le 7 Fév 2023
Modifié(e) : Fifteen12 le 7 Fév 2023
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 commentaire
Fifteen12
Fifteen12 le 7 Fév 2023
Modifié(e) : Fifteen12 le 7 Fév 2023
Note that this doesn't change the first label from 'feet' to 'foot'

Connectez-vous pour commenter.


Les Beckham
Les Beckham le 7 Fév 2023
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.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by