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

18 visualizaciones (últimos 30 días)
Behrooz Daneshian
Behrooz Daneshian el 7 de Feb. de 2023
Respondida: Les Beckham el 7 de Feb. de 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

Respuestas (2)

Fifteen12
Fifteen12 el 7 de Feb. de 2023
Editada: Fifteen12 el 7 de Feb. de 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 comentario
Fifteen12
Fifteen12 el 7 de Feb. de 2023
Editada: Fifteen12 el 7 de Feb. de 2023
Note that this doesn't change the first label from 'feet' to 'foot'

Iniciar sesión para comentar.


Les Beckham
Les Beckham el 7 de Feb. de 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.

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by