How to export a figure in Live Script?

88 visualizzazioni (ultimi 30 giorni)
free5721
free5721 il 30 Nov 2020
Commentato: free5721 il 3 Dic 2020
I am attempting to use the command:
saveas(gcf,'picture.png')
to save a figure from a Live Script. But to no success. An image file is NOT created at the destination.
The command works fine outside of Live Script. An image file IS created at the destination.
The only article that seemed to address the issue was...
but to no success.
Another article led me to speculate that perhaps it is not supported in Live Script.
Any thoughts? Thanks.
  1 Commento
Sindar
Sindar il 2 Dic 2020
Try this
print(gcf,'-dpng','picture.png')
I'm not sure if I actually printed anything from a livescript in 2018b, but this is my go-to method in general, and works for 2020a livescripts
Note that there are other issues with saving livescripts as .fig, and it's possible they are showing up here. You might try this (and if it works, do what I do and create a wrapper with the weirdness for each format so you rarely have to think about it again)
if ~strcmp(get(this_fig,'visible'),'on')
invisible = true;
set(gcf,"Visible","on");
end
print(gcf,'-dpng','picture.png')
if invisible
set(gcf,"Visible","off");
invisible = false;
end

Accedi per commentare.

Risposta accettata

free5721
free5721 il 2 Dic 2020
Modificato: free5721 il 2 Dic 2020
Thank you Sindar!
I used your first suggestion
print(gcf,'-dpng','picture.png')
within the Live Script Environment, and the figure was saved at the destination.
I would never have thought to use a print command. I looked up the print help menu and its summary says, "Print figure or save to specific file format" and an example it provides about saving a figure as PNG image is print('BarPlot','-dpng')
I looked up the saveas help menu and its summary says, "Save figure to specific file format"
and an example it provides about saving a figure as a PNG file is saveas(gcf,'Barchart.png').
So I wonder what the issue is between these two commands?
Nevertheless, thank again. Much appreciated! Have a good day!
  1 Commento
Sindar
Sindar il 2 Dic 2020
Yeah, I have no idea why one works and the other doesn't, but I've moved to using print for everything except .fig, where I use saveas

Accedi per commentare.

Più risposte (1)

Sindar
Sindar il 2 Dic 2020
Here's my wrapper function, with usage like
save_myfig(gcf,'spectrum_1e12',{'png','fig'})
I've been using some variation for versions since 2007b, and currently use for 2018b and 2020a, but I haven't extensively tested backwards compatibility
function save_myfig(this_fig,file_name,file_ext)
% Saves <this_fig> to <file_name> as a <file_ext>
% If multiple extensions are given, saves as each format
% If extension is not recognized, it tries anyway
% 'pdf_small' creates a low-resolution PDF
% make sure the figure has updated
drawnow
ext_options = {'fig','pdf','png','jpg','jpeg','eps','pdf_small'};
%% Parse inputs
% if no handle given, save the current figure
if nargin<1 || isempty(this_fig)
this_fig=gcf;
end
% if no name given, call it "figure.<ext>"
if (nargin<2 || isempty(file_name))
file_name='figure';
% trim off existing extensions
elseif ~isempty(file_name) && endsWith(file_name,ext_options)
[filepath,name,~] = fileparts(file_name);
file_name = [filepath filesep name];
end
% make sure the arguments are in the right format
file_name = convertStringsToChars(file_name);
if nargin<3 || isempty(file_ext)
file_ext={};
elseif ischar(file_ext)
file_ext={file_ext};
elseif isnumeric(file_ext)
file_ext=ext_options(file_ext);
end
%% check for each known extenion and save in that format
% JPG
if any(strcmpi(file_ext,'jpg')) || any(strcmpi(file_ext,'jpeg'))
print(this_fig,'-djpeg',[file_name '.jpg'])
end
% PDF (low resolution)
if any(strcmpi(file_ext,'pdf_small'))
% I always print in landscape, but this may not make sense with your figures
orient(this_fig,'landscape');
print(this_fig,'-dpdf','-r72','-bestfit',[file_name '.pdf']);
orient(this_fig,'portrait');
end
% PNG
if any(strcmpi(file_ext,'png'))
print(this_fig,'-dpng',[file_name '.png'])
end
% EPS
if any(strcmpi(file_ext,'eps'))
print(this_fig,'-depsc',[file_name '.eps'])
end
% PDF (normal resolution)
if any(strcmpi(file_ext,'pdf'))
% I always print in landscape, but this may not make sense with your figures
orient(this_fig,'landscape');
tmp=get(this_fig,'Position');
set(this_fig,'PaperUnits','points','PaperSize',tmp(3:4))
print(this_fig,'-dpdf','-r300',[file_name '.pdf']);
orient(this_fig,'portrait');
end
if any(strcmpi(file_ext,'fig'))
% clear warnings for the try-catch below
lastwarn('')
% check whether the figure is visible
invisible = false;
if ~strcmp(get(this_fig,'visible'),'on')
invisible = true;
set(this_fig,"Visible","on");
end
% try the normal way
try
saveas(this_fig, [file_name '.fig'],'fig')
catch
% warning('Variable ''d'', is larger than 10GB and could not be saved')
end
warnMsg = lastwarn;
% use v7.3 saving for large data
if ~isempty(warnMsg)
% warning('Fig Variable too large for v7, using v7.3 saving')
hgsave(this_fig, [file_name '.fig'], '-v7.3');
fprintf(['Fig actually saved\n'])
end
if invisible
set(this_fig,"Visible","off");
invisible = false;
end
end
% Try unrecognized file extensions
for ind = 1:length(file_ext)
if ~any(strcmp(file_ext{ind},ext_options))
print(this_fig,['-d' file_ext{ind}],[file_name '.' file_ext{ind}])
end
end
end
  2 Commenti
Sindar
Sindar il 2 Dic 2020
I know it works with 2020a livescripts, but not sure about earlier versions
free5721
free5721 il 3 Dic 2020
Thank you for the follow up and willingness to share. Your wrapper will be useful for my application.

Accedi per commentare.

Categorie

Scopri di più su Graphics Object Programming in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by