Problem by saveas plot

Hi everyone, I do not understand what I'am doing woring in saving my plot.
My code:
Temp=[num2str(vaiable), 'Myplotnumber' ,num2str(n) ,'-dpng'];
saveas(gcf,Temp);
it save the figure always with Var1Myplotnumber2.png.fig
why put matlab .fig at the end?
Thank you?

3 Comments

Use print.
doc print
CSCh
CSCh on 20 Dec 2020
If I use print(gca, Temp)
I got:
Error using alternatePrintPath
Invalid or unsupported printer '' specified.
Error in print (line 83)
pj = alternatePrintPath(pj);
Pass gcf and not axes
print(gcf, 'test', '-dpng', '-r400')

Sign in to comment.

 Accepted Answer

CSCh
CSCh on 4 Jan 2021

0 votes

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.
BR

More Answers (3)

Image Analyst
Image Analyst on 20 Dec 2020

0 votes

Use exportgraphics() instead if you have R2020a or later.

7 Comments

CSCh
CSCh on 20 Dec 2020
If I use
exportgraphics(f,Temp)
i got this error
Error using exportgraphics
When exporting to file specify both a file name and extension.
Hoewever Temp = "Var1Myplotnumber2.png" is working in the command line.
If I use:
hgexport it works without error, but I got:
Var1Myplotnumber2.png.eps
CSCh
CSCh on 20 Dec 2020
With f= figure(1)
How about if you do this:
g = gcf; % Or gca, whatever you want.
exportgraphics(g, Temp);
CSCh
CSCh on 20 Dec 2020
Got the same error message
Try this code in a simple script or in the command window.
plot(1:11, 'b-', 'LineWidth', 2);
fullFileName = fullfile(pwd, 'Delete me.png');
exportgraphics(gcf, fullFileName);
Does it create the file? For me it does.
CSCh
CSCh on 21 Dec 2020
Yes that works for me, but why does not my code work? Maybe something with my loop (num2str (n)), I have to check..
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.

Temp = {sprintf('%dMyplotnumber%d.png', vaiable, n),'-dpng'};
saveas(gcf, Temp{:})
You can do
baseFileName = sprintf('%d Myplotnumber %d.png', variable, n); % Name without folder.
fullFileName = fullfile(yourFolder, baseFileName); % Prepend folder.
saveas(gcf, fullFileName);

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2020a

Tags

Asked:

on 20 Dec 2020

Answered:

on 5 Jan 2021

Community Treasure Hunt

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

Start Hunting!