How to save contourf data and not plot it for running efficiency?

I have prepared data for finding a contour line on the contour map. However, I have to run thousands of simulations and most of the computation is used in opening and closing the figure. Is there a way that I can just save the contourf into the variable 'c' and not plot it.I am attaching the screenshot for your reference
I have attached the grid files in 'grid_data.mat' but it does not load in the online version. The code for my approach to plotting is given below:
load('grid_data.mat')
[c] = contourf(x1nan,y1nan,uanan,[33.33 33.33], 'ShowText','on',FaceAlpha=0.1,EdgeAlpha=0.8,EdgeColor='k',LevelStep=20);
I cannot get to load the file in this code, but I have attached it. Please let me know what i am doing wrong Than you in advance.

1 Comment

Answers is having problems with uploaded files.
I first noticed this two days ago and MathWorks knows about it. Apparently, it has not been fixed yet. (For whatever reason, we don’t have permission to access the uploaded files. Here, it mentiions that the file cannot be found. Using the websave function reveals that the file exists, however access to it is ‘Forbidden’, according to the error that is thrown when attempting that.)
file = websave('grid_data.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1820915/grid_data.mat')
Error using websave (line 107)
The server returned the status 403 with message "Forbidden" in response to the request to URL https://www.mathworks.com/matlabcentral/answers/uploaded_files/1820915/grid_data.mat.
MathWorks has been doing site maintenance recently. That apparently broke this capabiity (and a number of others).
.

Sign in to comment.

 Accepted Answer

Use gcf to return the figure handle, then set the visibility to 'off'.
Example —
[X,Y,Z] = peaks(50);
figure
[c,hc] = contourf(X,Y,Z, ShowText='on');
colormap(turbo)
colorbar
c % Contour Information
c = 2×522
-6.0000 0.3668 0.3061 0.1837 0.0612 0.0371 -0.0122 0.0454 0.0612 0.1837 0.3061 0.4286 0.4542 0.4637 0.4286 0.3668 -4.0000 0.5579 0.5510 0.4286 0.3061 0.1837 0.0612 -0.0612 -0.1837 -0.1965 -0.3061 -0.3114 -0.3526 -0.3421 15.0000 -1.7755 -1.8073 -1.8255 -1.7931 -1.7755 -1.6531 -1.5306 -1.5173 -1.4524 -1.4377 -1.5001 -1.5306 -1.6531 -1.7126 -1.7755 35.0000 -2.0204 -2.0250 -2.0839 -2.1178 -2.1304 -2.1221 -2.0907 -2.0297 -2.0204 -1.9054 -1.8980 -1.7755 -1.6531
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
[c,hc] = contourf(X,Y,Z);
c % Contour Information
c = 2×522
-6.0000 0.3668 0.3061 0.1837 0.0612 0.0371 -0.0122 0.0454 0.0612 0.1837 0.3061 0.4286 0.4542 0.4637 0.4286 0.3668 -4.0000 0.5579 0.5510 0.4286 0.3061 0.1837 0.0612 -0.0612 -0.1837 -0.1965 -0.3061 -0.3114 -0.3526 -0.3421 15.0000 -1.7755 -1.8073 -1.8255 -1.7931 -1.7755 -1.6531 -1.5306 -1.5173 -1.4524 -1.4377 -1.5001 -1.5306 -1.6531 -1.7126 -1.7755 35.0000 -2.0204 -2.0250 -2.0839 -2.1178 -2.1304 -2.1221 -2.0907 -2.0297 -2.0204 -1.9054 -1.8980 -1.7755 -1.6531
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ax = gcf;
Ax.Visible = 'off'; % Figure Not Displayed
See the documentatiion section on Visible for details.
EDIT — Added second documentation reference (Visible).
.

4 Comments

Thank you for the answer. I have a follow-up question. even if the axis visibility is off, it will still plot the curve and turn the figure off. By following this approach, does it help in speed or just adds another step?
ps. I just added the visible.off in the code line. I think this is what I wanted to do. I will check the optimization and get back. As always, thank you for your guidance. Really appreciated.
[c,hc] = contourf(X,Y,Z, ShowText='on',Visible='off');
pps. I just check it. The figure still opens up as an empty figure.
My pleasure!
It apparently does not draw the plot with Visible='off' since drawing the plot requires 0.2971 seconds, and not drawing it only requires 0.0563 seconds. This is repeatable, with subseequent runs yielding 0.4461.and 0.0722 and 0.7133 and 0.1248 and 0.7581 and 0.1258 seconds respectively.
[X,Y,Z] = peaks(50);
tic
figure
[c,hc] = contourf(X,Y,Z, ShowText='on');
colormap(turbo)
colorbar
c % Contour Information
c = 2×522
-6.0000 0.3668 0.3061 0.1837 0.0612 0.0371 -0.0122 0.0454 0.0612 0.1837 0.3061 0.4286 0.4542 0.4637 0.4286 0.3668 -4.0000 0.5579 0.5510 0.4286 0.3061 0.1837 0.0612 -0.0612 -0.1837 -0.1965 -0.3061 -0.3114 -0.3526 -0.3421 15.0000 -1.7755 -1.8073 -1.8255 -1.7931 -1.7755 -1.6531 -1.5306 -1.5173 -1.4524 -1.4377 -1.5001 -1.5306 -1.6531 -1.7126 -1.7755 35.0000 -2.0204 -2.0250 -2.0839 -2.1178 -2.1304 -2.1221 -2.0907 -2.0297 -2.0204 -1.9054 -1.8980 -1.7755 -1.6531
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
t(1) = toc
t = 0.7581
figure
[c,hc] = contourf(X,Y,Z);
c % Contour Information
c = 2×522
-6.0000 0.3668 0.3061 0.1837 0.0612 0.0371 -0.0122 0.0454 0.0612 0.1837 0.3061 0.4286 0.4542 0.4637 0.4286 0.3668 -4.0000 0.5579 0.5510 0.4286 0.3061 0.1837 0.0612 -0.0612 -0.1837 -0.1965 -0.3061 -0.3114 -0.3526 -0.3421 15.0000 -1.7755 -1.8073 -1.8255 -1.7931 -1.7755 -1.6531 -1.5306 -1.5173 -1.4524 -1.4377 -1.5001 -1.5306 -1.6531 -1.7126 -1.7755 35.0000 -2.0204 -2.0250 -2.0839 -2.1178 -2.1304 -2.1221 -2.0907 -2.0297 -2.0204 -1.9054 -1.8980 -1.7755 -1.6531
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ax = gcf;
Ax.Visible = 'off'; % Figure Not Displayed
t(2) = toc
t = 1×2
0.7581 0.8839
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
dt = diff([0 t])
dt = 1×2
0.7581 0.1258
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If my Answer helped you solve your problem, please Accept it!
EDIT — (13 Dec 2024 at 14:28)
Getting the information from a contour plot matrix is a bit challenging, hoiwever not difficult.
Example —
figure
[M,C] = contourf(X, Y, Z, 'ShowText',1);
Levels = C.LevelList
Levels = 1×9
-6.5259 -6.0000 -4.0000 -2.0000 0 2.0000 4.0000 6.0000 8.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for k = 1:numel(Levels)
idx = find(M(1,:) == Levels(k));
ValidV = rem(M(2,idx),1) == 0;
StartIdx{k,:} = idx(ValidV);
VLen{k,:} = M(2,StartIdx{k});
end
figure
k1 = 4; % Index For Levels 'k1'
hold on
for k2 = 1:numel(StartIdx{k1})
idxv = StartIdx{k1}(k2)+1 : StartIdx{k1}(k2)+VLen{k1}(k2); % Index For Contour 'k1'
xv = M(1,idxv);
yv = M(2,idxv);
plot(xv, yv)
end
hold off
xlabel('M(1,:)')
ylabel('M(2,:)')
title(sprintf('Contour Level %.1f', Levels(k1)))
axis('equal')
This example plots only one contour (for simplicity), however that appears to be what you want to do.
.
As always. Thank you very much for your detailed answer. For now I will improve the speed of analysis by turning off the visibility. I will still keep looking for alternatives. If I find one, I will update here.
Thanks a bunch.
ps. I have opened a figure at the start of the loop. Probably it will save some more time in not opening and closing the figure.
As always, my pleasure!
If you siimply want to over-write the figure in the next iterration, there is no need to close it first. Over-wertinng it will do essentially the same operation.

Sign in to comment.

More Answers (0)

Products

Release

R2024a

Asked:

on 13 Dec 2024

Commented:

on 14 Dec 2024

Community Treasure Hunt

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

Start Hunting!