代码之家  ›  专栏  ›  技术社区  ›  havakok

将图像插入MATLAB中的excel单元格

  •  4
  • havakok  · 技术社区  · 7 年前

    this post

    im 是矩阵而不是句柄:

    print(im, '-dbitmap');
    

    我需要为图像创建一个句柄吗?还有更好的方法吗?

    最后,我想改变细胞,使其能够适应周围的图像(不改变图像的大小)。

    2 回复  |  直到 7 年前
        1
  •  5
  •   gnovice    7 年前

    print 语句打印 figure window

    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
    

    create a COM server 并将图形打印到Excel文件中,如下所示:

    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
    

    上面尝试缩放单元格以适应整个图像。这适用于行高,但由于某些原因,列宽处于禁用状态。由于列宽是根据字符定义的,图像宽度是根据点/英寸定义的,因此确定适当的缩放比例似乎有很多困难。我不确定是否有好的解决方法。

    作为一个例子,下面是示例MATLAB图像的结果 'peppers.png' :

    enter image description here

        2
  •  3
  •   Sardar Usama    7 年前

    gnovice answer . 写这个答案对我真的很有帮助。


    peppers.png 在牢房里 B2 :

    im=imread('peppers.png');
    imshow(im);
    
    dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
    print(gcf, sprintf('-r%d', dpi), ...      % Print the figure at the screen resolution
          '-clipboard', '-dbitmap');          %   to the clipboard as a bitmap
    
    excel = actxserver('Excel.Application');  % Create server object
    excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
    excelSheet = excel.ActiveSheet;           % Get the active sheet
    excelSheet.Range('B2').PasteSpecial();    % Paste from clipboard (top left corner
                                              %   of image will be in the cell 'B2')   
    %%%%%%%%%%%%%%%% My contribution %%%%%%%%%%%%%%%%
    excelSheet.Shapes.Item(1).LockAspectRatio='msoFalse';            %Unlocking aspect ratio
    excelSheet.Shapes.Item(1).Width=excelSheet.Range('B2').Width;    %Adjusting width
    excelSheet.Shapes.Item(1).Height=excelSheet.Range('B2').Height;  %Adjusting height
    %Uncomment the next line if you want the cell to keep the image fit in its particular 
    %cell even if the size of the cell is changed later
    % excelSheet.Shapes.Item(1).Placement='xlMoveandSize';
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    excelWorkbook.SaveAs('figtest.xlsx');     % Save workbook to a file
    excelWorkbook.Close();                    % Close workbook
    excel.Quit();                             % Quit server
    excel.delete();                           % Delete server object
    

    输出:

    output