Clear Filters
Clear Filters

Exportgraphics is not overwriting

59 views (last 30 days)
Vivek
Vivek on 13 Jul 2024 at 4:57
Edited: DGM on 13 Jul 2024 at 8:33
I have a set of figures that I'm saving using exportgraphics, after making some changes to the programm i get a new set of figures but exportgraphics is not overwriting it. I have been using this to export images for a long time and it used to overwrite, but since yesterday it isn't doing so. Any ideas about what may be causing this?
  3 Comments
Vivek
Vivek on 13 Jul 2024 at 5:31
This is how i'm using it,
clc
clear
close all
f2 = "D:\DesiredFolder\";
x = 1:100;
y = 100:-1:1;
fig = figure;
plot(x,y)
fig.Units='inches';
fig.Position=[0,0,8,8.5];
filenameEN = fullfile(f2,"xyz.jpg");
exportgraphics(fig, filenameEN,'Resolution', 120);
Yes, I have checked timestamps,it is not changing. It is saving images only when I delete previous images, so location is not a problem.
DGM
DGM on 13 Jul 2024 at 8:29
Edited: DGM on 13 Jul 2024 at 8:33
f2 = ""; % just for forum testing
filenameEN = fullfile(f2,"xyz.jpg");
x = 1:100;
y = 100:-1:1;
fig = gcf;
% i'm going to repeatedly plot and save the screenshot
for k = 1:3
plot(x,y)
fig.Units='inches';
fig.Position=[0,0,8,8.5];
exportgraphics(fig, filenameEN,'Resolution', 120);
imfinfo(filenameEN).FileModDate % check the timestamp
pause(3)
end
ans = '13-Jul-2024 08:19:06'
ans = '13-Jul-2024 08:19:09'
ans = '13-Jul-2024 08:19:13'
I'm not sure why the forum editor dumps the figure twice here, but all I'm looking for are the timestamps, which do change.
My point in testing this here on the forum isn't strictly demonstrative; it's simply that I run R2019b, which doesn't have exportgraphics(), so the forum is where I'd have to test it.
From my vantage point, I don't see a good reason why exportgraphics() wouldn't write the file, though I think there's probably an explanation that I simply can't see from where I'm at. For example, I've had scenarios where my file manager simply stopped updating the contents of the directory I was looking at. It might be worth trying to use imfinfo() as above to programmatically check the file for changes.
That said, I would hope that someone else can chime in with more insight on newer versions.
One last note which is completely unrelated to the main problem:
I strongly recommend using PNG for capturing line plots like this. JPG will produce a visually degraded copy and a larger file size than a PNG. Unless you need JPG for compatibility reasons, it's best avoided for simple synthetic images. Again, that shouldn't be affecting the file-overwriting issue at all. Just general advice.

Sign in to comment.

Answers (0)

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!