我正在跟随这篇文章,我需要做类似的事情,只是我想把一个图像(m x n x 3矩阵)放入Excel中的单元格中。
因为我的图像
因为我的图像
im
是一个矩阵而不是句柄,所以这行代码不起作用:print(im, '-dbitmap');
我需要为这张图片创建一个句柄吗?是否有其他更好的方法?
最终,我希望更改单元格的大小以适应图片(而不更改图片的大小)。
im
是一个矩阵而不是句柄,所以这行代码不起作用:print(im, '-dbitmap');
我需要为这张图片创建一个句柄吗?是否有其他更好的方法?
最终,我希望更改单元格的大小以适应图片(而不更改图片的大小)。
print
语句将图形窗口的内容打印到文件中,因此您需要先绘制图像:
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
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
excelSheet.Range('B2').Height = excelSheet.Shapes.Item(1).Height;
。我会尝试找到一个解决方法。 - havakokpeppers.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
输出: