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

11 views (last 30 days)
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

Answers (2)

Fifteen12
Fifteen12 on 7 Feb 2023
Edited: Fifteen12 on 7 Feb 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

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

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!