在 Excel 中打印 MATLAB 图窗并调整其大小

发布于 2024-11-17 19:08:36 字数 327 浏览 1 评论 0原文

我在 MATLAB 中有两个图形,其句柄为 hFig1hFig2。我想将它们打印到 Excel 中的特定单元格(单元格 E3 和 I3),并将它们重新调整为 [2 英寸 x 3 英寸]。

我尝试过使用 .AddPictures 对象处理程序和 print -dmeta,但我找不到实现所有三个目标的方法。

我同时也在向excel写入数据,并且由于发送的数据行很多,所以我希望有一种不需要通过ActiveXServer不断调用excel的方法。

有没有人有解决此类问题的好方法或资源?

I have two figures in MATLAB with the handles hFig1 and hFig2. I would like to print them to specific cells in Excel (cells E3 and I3) and reshape them to each be [2in x 3in].

I have tried using the .AddPictures object handler and using print -dmeta, but I can't find a way to achieve all three of my objectives.

I am also writing data to excel at the same time and because there is a lot of data lines being sent, I was hoping to have a method that didn't require continually invoking excel with the ActiveXServer.

Does anyone have a good method or resource for this kind of problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-11-24 19:08:37

您可以最轻松地将 MATLAB 数字添加到 Excel,如下所示:

% Create some arbitrary graphics
f1 = figure; peaks; f2 = figure; membrane;

% Connect to Excel, make it visible and add a worksheet
xl = actxserver('Excel.Application'); set(xl,'Visible',1);
xl.Workbooks.Add(1); xls = xl.ActiveSheet;

% Paste in the MATLAB figures
print(f1, '-dbitmap'); xls.Range('E3').PasteSpecial;
print(f2, '-dbitmap'); xls.Range('I3').PasteSpecial;

我不确定将图像设为 2 英寸 x 3 英寸到底是什么意思。是屏幕上的尺寸吗?以什么分辨率?还是打印时的尺寸?

您可以直接以点为单位指定图像的大小和位置:

xls.Shapes.Item(1).PictureFormat.CropLeft  = 30;
xls.Shapes.Item(1).PictureFormat.CropRight  = 30;
xls.Shapes.Item(1).Height  = 200;
xls.Shapes.Item(1).Left = xls.Range('E3').Left;

但如果您想要以英寸为单位,则需要一种转换为点的方法,具体取决于其显示方式。

您提到您不断使用 actxserver 调用 Excel;每次写入每条数据时都要一遍又一遍地连接吗?您可能不需要这样做 - 更有可能的是,您可以保持单个连接打开并将每条数据写入其中,然后保存文件并关闭连接。

Excel 对象模型的一个很好的参考资源是 Microsoft 文档。但这并不容易阅读:)

You can add MATLAB figures to Excel most easily like this:

% Create some arbitrary graphics
f1 = figure; peaks; f2 = figure; membrane;

% Connect to Excel, make it visible and add a worksheet
xl = actxserver('Excel.Application'); set(xl,'Visible',1);
xl.Workbooks.Add(1); xls = xl.ActiveSheet;

% Paste in the MATLAB figures
print(f1, '-dbitmap'); xls.Range('E3').PasteSpecial;
print(f2, '-dbitmap'); xls.Range('I3').PasteSpecial;

I'm not sure exactly what you mean by making the images 2in by 3in. Is that the size on screen? At what resolution? Or is it the size when printed?

You can specify the size and position of the image in points directly:

xls.Shapes.Item(1).PictureFormat.CropLeft  = 30;
xls.Shapes.Item(1).PictureFormat.CropRight  = 30;
xls.Shapes.Item(1).Height  = 200;
xls.Shapes.Item(1).Left = xls.Range('E3').Left;

But if you want it in inches you'll need a way of converting to points, depending on the way it's displayed.

You mention that you're continually invoking Excel with actxserver; are you connecting over and over again each time you write each piece of data? You probably don't need to do that - more likely you could keep a single connection open and write each piece of data to it, then save the file and close the connection.

A good reference resource for the Excel Object Model is the Microsoft documentation. It's not an easy read though :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文