以编程方式导出数字(R2019a)

问题描述 投票:4回答:1

在MATLAB R2019a中,一个new way to export figures was added,其结果是“围绕轴紧密裁剪,具有最小的空白”。使用轴工具栏访问此功能:

enter image description here

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

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

croppedExport(hAxes, outputPath);

附: 我知道可以使用export_fig等第三方工具实现此功能。

matlab user-interface export ui-automation undocumented-behavior
1个回答
4
投票

TL;DR

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图像!

enter image description here

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

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

......哪个有效(!),所以这成了我们选择的方法。

© www.soinside.com 2019 - 2024. All rights reserved.