In MATLAB R2019a a new way to export figures was added whereby the result "is tightly cropped around the axes with minimal white space". This feature is accessed using the axes toolbar:
My question is: How do we invoke this new export feature programmatically?
It should be fairly easy to get the export dialog to open for specific axes (i.e. simulate a button click), but I'm more interested in bypassing the dialog and just saving a file to disk, e.g.
croppedExport(hAxes, outputPath);
P.S.
I am aware that this functionality can be achieved using a 3rd party tool like export_fig
.
matlab.graphics.internal.export.exportTo(hAxes, fullpath);
The tooltip of this new button says "Export...", which is going to help us identify it. Digging within the properties of the axes toolbar (struct(hAxes.Toolbar)
) we can get a glimpse of the function that is being called when the button is pressed:
hB = struct(struct(hAxes.Toolbar).ButtonGroup).NodeChildren(1);
%{
hB =
ToolbarPushButton (Export...) with properties:
Tooltip: 'Export...'
Icon: 'export'
ButtonPushedFcn: @(e,d)matlab.graphics.internal.export.exportCallback(d.Axes)
%}
which unfortunately points to directory full of .p
files:
...\MATLAB\R2019a\toolbox\matlab\graphics\+matlab\+graphics\+internal\+export
...and forces us to do proceed with trial and error. For example, we can choose a random .p
file whose name sounds right to us, and see if we can discover its API:
>> matlab.graphics.internal.export.exportTo()
Error using matlab.graphics.internal.export.exportTo
Not enough input arguments.
>> matlab.graphics.internal.export.exportTo('')
Error using matlab.graphics.internal.export.exportTo
Not enough input arguments.
>> matlab.graphics.internal.export.exportTo('','')
Error using matlab.graphics.internal.export.ExporterArgumentParser/parseInputParams
'' matches multiple parameter names: 'background', 'destination', 'format', 'handle', 'margins', 'resolution', 'target'. To avoid ambiguity, specify the complete name of the parameter.
Error in matlab.graphics.internal.export.ExporterArgumentParser/processArguments
Error in matlab.graphics.internal.export.Exporter/process
Error in matlab.graphics.internal.export.exportTo
The last error message provides very interesting information, which allows us to take some educated guesses regarding the required inputs:
'background' - probably background color 'destination' - probably where to put the file 'format' - probably what is the file extension 'handle' - probably the axes handle 'margins' - (self explanatory) 'resolution' - (self explanatory) 'target' - ???
Following the "minimal" input set that is requested in the question, our next attempt is:
membrane;
matlab.graphics.internal.export.exportTo('handle', gca, 'destination', 'e:\blabla.png');
... which creates the a file at the desired location and also returns a truecolor RGB image that is cropped just like we wanted!
Although we are done, we can try to simplify this function call even further based on the "convention" of saveas
, which is saveas(what, where, ...)
:
matlab.graphics.internal.export.exportTo(gca, 'e:\blabla.png');
... which works (!) and so this becomes our method of choice.