MATLAB: Saving figure in app designer.

app designerfigurelegendMATLAB

Hi, I want to save a figure in app designer called app.ScanDataPlot.
The code below saves the figure (but just the data, with x and y axes but with no titles). I also want to include the legend of the graph (for the different curves) and include the x and y axes titles. How can I do this?
The code does not save the figure as .png, just as .fig. Any ideas how to get the figure in .png format also?
% Create a temporary figure with axes.
figScan = figure;
figAxes = axes(figScan);
% Copy all UIAxes children, take over axes limits and aspect ratio.
allChildren = app.ScanDataPlot.XAxis.Parent.Children;
copyobj(allChildren, figAxes)
figAxes.XLim = app.ScanDataPlot.XLim;
figAxes.YLim = app.ScanDataPlot.YLim;
figAxes.ZLim = app.ScanDataPlot.ZLim;
figAxes.DataAspectRatio = app.ScanDataPlot.DataAspectRatio;
% Save as png and fig files.
fileName = fullfile([pathway,'\data','\fig-file\'], 'WorstLocation.fig');
saveas(figScan, fileName, 'png');
savefig(figScan, fileName);

Best Answer

You might want to take a look at this contribution:
Regarding the saving as png: I guess that it does not work because fileName already has the fig extension.
Could you give this a try:
fileName = fullfile([pathway,'\data','\fig-file\'], 'WorstLocation');
saveas(figScan, [fileName '.png'], 'png');
savefig(figScan, [fileName '.fig']);
Related Question