4

在 MATLAB R2019a中添加了一种导出图形的新方法,结果“以最小的空白围绕轴紧密裁剪”。使用轴工具栏访问此功能:

在此处输入图像描述

我的问题是:我们如何以编程方式调用这个新的导出功能?

为特定轴打开导出对话框应该相当容易(即模拟按钮单击),但我更感兴趣的是绕过对话框并将文件保存到磁盘,例如

croppedExport(hAxes, outputPath);


PS
我知道这个功能可以使用 3 rd方工具来实现,比如export_fig.

4

1 回答 1

4

TL;博士

matlab.graphics.internal.export.exportTo(hAxes, fullpath);

这个新按钮的工具提示显示“导出... ”,这将帮助我们识别它。深入研究坐标区工具栏 ( struct(hAxes.Toolbar)) 的属性,我们可以看到按下按钮时调用的函数:

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)
%}

不幸的是,它指向充满.p文件的目录:

...\MATLAB\R2019a\toolbox\matlab\graphics\+matlab\+graphics\+internal\+export

...并迫使我们继续反复试验。例如,我们可以选择一个.p名字听起来对的随机文件,看看我们是否可以发现它的 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 

最后一条错误消息提供了非常有趣的信息,这使我们能够对所需的输入进行一些有根据的猜测:

'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'      - ???

在问题中要求的“最小”输入集之后,我们的下一个尝试是:

membrane;
matlab.graphics.internal.export.exportTo('handle', gca, 'destination', 'e:\blabla.png');

...它在所需位置创建一个文件,并返回一个真彩色 RGB 图像,该图像像我们想要的那样被裁剪!

在此处输入图像描述

虽然我们已经完成了,但我们可以尝试根据 的“约定”进一步简化这个函数调用saveas,即saveas(what, where, ...)

matlab.graphics.internal.export.exportTo(gca, 'e:\blabla.png');

... 哪个有效(!),因此这成为我们选择的方法。

于 2019-03-24T14:52:56.800 回答