将图像插入到MATLAB中的excel单元格中 [英] Insert an image into a excel cell in MATLAB

查看:818
本文介绍了将图像插入到MATLAB中的excel单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注


I was following this post and I need to do about the same thing only I want to put an image (m by n by 3 matrix) into a cell in excel.

this line wont work because my image im is a matrix and not a handle:

print(im, '-dbitmap');

Do I need to somehow create a handle for the image? Is there another\better way?

Eventually I want to change the cell so that it can fit around the image (without changing the image's size).

解决方案

The print statement prints the contents of a figure window to a file, so you have to plot your image first:

image(im);                        % Plot image
set(gca, 'Visible', 'off', ...    % Turn off axes visibility
         'Position', [0 0 1 1]);  %   and make axes fill figure window
hFigure = gcf;                    % Get handle to figure
pos = get(hFigure, 'Position');   % Get current figure position
set(hFigure, 'Position', [pos(1:2) size(im, 2) size(im, 1)]);  % Set position so image
                                                               %   is scaled properly

Then you can create a COM server and print the figure to an Excel file like so:

excel = actxserver('Excel.Application');  % Create server object
excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
excelSheet = excel.ActiveSheet;           % Get the active sheet

dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
print(hFigure, sprintf('-r%d', dpi), ...  % Print the figure at the screen resolution
      '-clipboard', '-dbitmap');          %   to the clipboard as a bitmap
excelSheet.Range('B2').PasteSpecial();    % Paste from clipboard (top left corner
                                          %   of image will be in the cell 'B2')

excelSheet.Range('B2').RowHeight = ...    % Set cell height to image height
  excelSheet.Shapes.Item(1).Height;
widthScale = excelSheet.Range('B2').ColumnWidth./...  % Column width (in characters)
             excelSheet.Range('B2').Width;            % Column width (in points)
excelSheet.Range('B2').ColumnWidth = ...  % Set cell width to scaled image width
  excelSheet.Shapes.Item(1).Width.*widthScale;

excelWorkbook.SaveAs('figtest.xlsx');  % Save workbook to a file
excelWorkbook.Close();                 % Close workbook
excel.Quit();                          % Quit server
excel.delete();                        % Delete server object

The above attempts to scale the cell to fit the entire image. This works for the row height, but the column width is off for some reason. There seems to be a lot of difficulty in determining the proper scaling, since the column width is defined in terms of characters and the image width is defined in terms of points/inches. I'm not sure if there's a good workaround for this.

As an example, here's what the results look like for the sample MATLAB image 'peppers.png':

这篇关于将图像插入到MATLAB中的excel单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆