Problem by saveas plot

10 views (last 30 days)
CSCh on 20 Dec 2020
Answered: Image Analyst on 5 Jan 2021
Hi everyone, I do not understand what I'am doing woring in saving my plot.
My code:
Temp=[num2str(vaiable), 'Myplotnumber' ,num2str(n) ,'-dpng'];
it save the figure always with Var1Myplotnumber2.png.fig
why put matlab .fig at the end?
Thank you?
Ive J
Ive J on 20 Dec 2020
Pass gcf and not axes
print(gcf, 'test', '-dpng', '-r400')

Sign in to comment.

Accepted Answer

CSCh on 4 Jan 2021
Thank you all so much for your help. In my case the '-dpng' ending did not work.
if I change to:
Temp=['%MyName%' ,'.png'];
saveas(gcf, Temp)
it saves png format.

More Answers (3)

Image Analyst
Image Analyst on 20 Dec 2020
Use exportgraphics() instead if you have R2020a or later.
Image Analyst
Image Analyst on 21 Dec 2020
I don't know. You never showed me your exportgraphics() code that didn't work. You only showed it where it worked in the command window with a hard-coded filename string. Please post the non-working code from your m-file. Apparently it thinks your Temp did not have an extension on it. Are you sure Temp ends in '.png'?

Sign in to comment.

Walter Roberson
Walter Roberson on 21 Dec 2020
Temp = {sprintf('%dMyplotnumber%d.png', vaiable, n),'-dpng'};
saveas(gcf, Temp{:})

Image Analyst
Image Analyst on 5 Jan 2021
You can do
baseFileName = sprintf('%d Myplotnumber %d.png', variable, n); % Name without folder.
fullFileName = fullfile(yourFolder, baseFileName); % Prepend folder.
saveas(gcf, fullFileName);





Community Treasure Hunt

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

Start Hunting!