MATLAB Answers

Is there a way to save snapshots of figures in a LiveScript without keeping the figure objectss open in a for loop?

6 views (last 30 days)
I have a plotting function in a for loop to go through data and produce a plot for each of them. Let's say the resultant figure contains huge amount of graphic objects, and very heavy in memory.
for i = 1:20
figure
plot(rand(10,1),rand(10,1))
end
In such cases, it would be useful, if one can keep only the screenshots of figures in a Live Script without keeping the bulky invisible figure objects open.
Hypothetically, if the code below could allow you to save snapshots in a Live Script, without actually keeping the figure object active, that might be convinient. The problem is that the code below will just close all the figures and nothing is shown in a Live Script.
for i = 1:20
figure
plot(rand(10,1),rand(10,1))
drawnow
close
end
This is the closest I could found. Actual plotting is postponed until the for loop finishses. But still the invisible figure objects are accumulating during the for loop.
for i = 1:20
figure
plot(rand(10,1),rand(10,1))
drawnow limitrate
end
drawnow
close all
Does anyone know to if keeping snapshots in a LiveScript without keeping figure objects is possible?

  0 Comments

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 9 Mar 2020
Edited: Fangjun Jiang on 10 Mar 2020
use saveas() to save the figure image.
test1.mlx, file size is 753k. 20 figures are saved. Each contains individual figure objectives.
for k=1:20
h=figure;
plot(rand(3));
end
test2.mlx, file size is 666k. 20 figures are saved but each is an image.
for k=1:20
h=figure;
plot(rand(3));
saveas(h,'test.png');
imshow('test.png');
end
When your data is really large and complex, maybe the .mlx file with image could be much smaller.

  3 Comments

Kouichi C. Nakamura
Kouichi C. Nakamura on 9 Mar 2020
Thanks, but ... you totally missed my point. Question is not about how to save figure by using saveas. Rather, it's about how to save "snapshots" in LiveScripts without keeping figure objects. I want to use LiveScript to keep record of what I'm doing. Probably there is no way...
Kouichi C. Nakamura
Kouichi C. Nakamura on 10 Mar 2020
Thanks. It took a while to get your idea, but yes, this really works.
Suppose I have hundreds of thousands of objects in each figure, I save them as a small PNG by saveas in each for loop, and immediately use imshow to store those images in the Live Scrpt.
Brilliant, thanks!
Saving an image and reopening it take some overhead.
Although tic-toc measurement is not accurate in LiveScript, it still tells us something.
t1 = zeros
for i = 1:10
close all
tic
for k=1:20
h=figure;
plot(rand(3));
end
t1(i) = toc
end
mean(t1)
median(t1)
std(t1)
Elapsed time is 1.11 ± 0.15 seconds (mean ± SD).
clear; close all
tic
for k=1:20
h=figure;
plot(rand(3));
saveas(h,'test.png');
close(h)
figure
ax = axes;
imshow('test.png','Parent',ax);
end
toc
Elapsed time is 45.4 ± 37.0 seconds (mean ± SD).
The overhead time was ~2 seconds per loop.

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Translated by