Exportgraphics is not overwriting

39 次查看(过去 30 天)
Vivek
Vivek 2024-7-13
回答: Sandeep Mishra 2024-9-3,8:08
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 个评论
Vivek
Vivek 2024-7-13
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 2024-7-13
编辑:DGM 2024-7-13
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.

请先登录,再进行评论。

回答(1 个)

Sandeep Mishra
Sandeep Mishra 2024-9-3,8:08
Dear Vivek,
Upon running the code snippet in MATLAB R2022a, I noticed that the exportgraphics function is working properly and overwriting the file when executed multiple times.
To address the issue, consider the following troubleshooting steps:
  1. Use the clear allcommand to remove any existing variables that might interfere with your script.
  2. Make sure the file you are trying to overwrite is not open in another application, which can prevent overwriting.
Additionally, a practical solution can be to delete the existing file before exporting the new image.
You can achieve this with the following MATLAB code snippet:
if exist(filenameEN, 'file')
delete(filenameEN);
end
exportgraphics(gca, filenameEN);
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by