error in saving overwritten images

I am doing image analysis of particle tracking in a fluidized bed. Part of my code is to produce the figure of particle trajectory then save it as png and then finding the covered area. In order to avoid opening too many windows I invisibled the figure and overwrite the saved png images. But after about 900 iteration and images i get this error:
Error using name (line 102)
Cannot create output file '.\fig.png'.
Error in print (line 71)
pj = name( pj );
Error in saveas (line 168)
print( h, name, ['-d' dev{i}] )
Error in particle_centriod_mark4 (line 61)
saveas(f,'fig.png');
The part of the code which makes the images is a as below:
f = figure('visible','off');
plot(part_cen_x(1:i),part_cen_y(1:i),'k-o','LineWidth', 14)
xlim([30 440]);
ylim([30 640]);
axis off
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
saveas(f,'fig.png');

回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 11 月 28 日

0 投票

As a test, try changing
saveas(f,'fig.png');
to
try
saveas(f,'fig.png');
catch ME
fprintf('There was a problem saving the file.\n');
fprintf('List of open files is:\n')
fopen('all')
fprintf('End of list of open files\n');
rethrow(ME)
end
This tests the possibility that MATLAB might be failing to close files, and shows a list of all of the open files. If there are more than a very small number of files listed then MATLAB might be "leaking" files.

5 件のコメント

hamed
hamed 2017 年 11 月 29 日
It didn't work and the simulation even crashed (but maybe due to my computer issues)
Walter Roberson
Walter Roberson 2017 年 11 月 29 日
When you say "didn't work" do you mean that nothing changed, or that you received the "There was a problem saving the file" message?
hamed
hamed 2017 年 11 月 29 日
Actually I couldn't see any error message as the whole Matlab window turned black. I think my computer also has some issues. I tried to use close(f) after saveas and the simulation is still running. let's see how this goes:
f = figure('visible','off');
plot(part_cen_x(1:i),part_cen_y(1:i),'k-o','LineWidth', 14)
xlim([30 440]);
ylim([30 640]);
axis off
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
saveas(f,'fig.png');
close(f)
hamed
hamed 2017 年 11 月 29 日
編集済み: hamed 2017 年 11 月 29 日
still problem exists. However, this time progresses more up to 2750 iterations. I terminated the program using Ctrl+c as it took so long and seems not moving forward. This is the error:
MATLAB has experienced a low-level graphics error, and may not have drawn correctly.
Read about what you can do to prevent this issue at Resolving Low-Level Graphics Issues then restart MATLAB.
To share details of this issue with MathWorks technical support,
please include this file with your service request.
Operation terminated by user during alternatePrintPath
In alternatePrintPath
In print (line 82)
pj = alternatePrintPath(pj);
In saveas (line 168)
print( h, name, ['-d' dev{i}] )
In particle_centriod_mark4 (line 64)
saveas(f,'fig.png');
hamed
hamed 2017 年 11 月 29 日
this is the code that finally gives the area fraction of colored area to white area:
f = figure('visible','off');
plot(part_cen_x(1:i),part_cen_y(1:i),'k-o','LineWidth', 14)
xlim([30 440]);
ylim([30 640]);
axis off
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
saveas(f,'fig.png');
close(f)
% try
% saveas(f,'fig.png');
% catch ME
% fprintf('There was a problem saving the file.\n');
% fprintf('List of open files is:\n')
% fopen('all')
% fprintf('End of list of open files\n');
% rethrow(ME)
% end
I=imread('fig.png');
I=rgb2gray(I);
BW = imbinarize(I);
no_white_pixels=bwarea(BW);
numberOfPixels = numel(I);
Area_fraction2(i,1)=1-no_white_pixels/numberOfPixels;

サインインしてコメントする。

Image Analyst
Image Analyst 2017 年 11 月 29 日

0 投票

I wonder if your axes are just getting really overstuffed with content. Can you try to do this before you plot to each one:
axes(whateverHandle); % Switch to the axes you want to plot new stuff in.
hold off;
cla reset
% plot() or imshow()....

9 件のコメント

hamed
hamed 2017 年 11 月 29 日
Undefined function or variable 'whateverHandle'.
Error in particle_centriod_mark4 (line 49)
axes(whateverHandle); % Switch to the axes you want to plot new stuff in.
hamed
hamed 2017 年 11 月 29 日
I'm using MATLAB R2017a academic use.
Walter Roberson
Walter Roberson 2017 年 11 月 29 日
Change whateverHandle to the handle of the axis you are plotting in.
hamed
hamed 2017 年 11 月 29 日
What's exactly the command for that?
Walter Roberson
Walter Roberson 2017 年 11 月 30 日
編集済み: Walter Roberson 2017 年 11 月 30 日
whateverHandle = ax;
Image Analyst
Image Analyst 2017 年 11 月 30 日
Let's say you're using GUIDE and you have two axes, one called axes1 and the other called axes2. Let's say you want to make the plots in axes2, so you'd do
axes(handles.axes2);
This will make sure that everything (plots, title, axis labels, etc.) takes place in axes2, not axes1. If you have only one plot/axes, then just use gca, or get rid of the axes() call altogether.
hamed
hamed 2017 年 11 月 30 日
I used handle(axes) and it's still running let's see how this goes.
Image Analyst
Image Analyst 2017 年 11 月 30 日
That won't work. Note, I did not do that. handles is a structure, not an structure array, and axes is the name of a built-in function. So calling handles(axes) will throw all kinds of errors.
hamed
hamed 2017 年 12 月 1 日
non of above mentioned code worked as Matlab didn't recognize the function or command:
whateverHandle = ax;
axes(handles.axes2);

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

質問済み:

2017 年 11 月 28 日

コメント済み:

2017 年 12 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by