Writing MATLAB image to Excel file at a specific position

조회 수: 33 (최근 30일)
Homayoun
Homayoun 2012년 6월 26일
댓글: 효준 2024년 9월 11일
I have a function like [summary,image1,image2,image3] = funca(x,y). I wonder how I can write each image to a separate sheet in an Excel workbook at a specific position. Thanks for your comments!

답변 (2개)

Noam Greenboim
Noam Greenboim 2019년 5월 13일
You can try to use this function I made:

Image Analyst
Image Analyst 2012년 6월 27일
MATLAB can't do this directly. You'd need to use ActiveX programming to call Excel API functions. Fortunately MATLAB can do that (make ActiveX calls). I don't recall doing that (stuffing images into Excel) so I don't have any demo code for you.
It's going to look somewhat similar to this:
% Open Excel file.
objExcel = actxserver('Excel.Application');
objExcel.Workbooks.Open(fullfile(excelFilePath, excelFileName)); % Full path is necessary!
oWB = objExcel.Workbooks.Add();
oSheet = oWB.ActiveSheet;
oSheet.Range("A1").Select;
oSheet.Pictures.Insert("<< full path to image file >>").Select();
But you can look up ActiveX in Answers or elsewhere. Here's one example:
The Microsoft Excel methods you need are listed here:
This code may also be useful:
  댓글 수: 3
Morteza
Morteza 2018년 10월 23일
I faced an error during compiling the above code: "Undefined function or variable 'Pictures'"
Image Analyst
Image Analyst 2018년 10월 24일
Strange - that is the command that gets recorded if you record a macro. But I was also able to reproduce the error. Not sure why it errored out but I found an alternate way. I've tested this and it works:
% Get the name of the workbook you want to paste a picture into.
folder = pwd;
excelFileName = 'PicturePaste.xlsx';
fullFileName = fullfile(folder, excelFileName);
if ~exist(fullFileName, 'file')
message = sprintf('Existing Excel workbook not found"\n%s', fullFileName);
uiwait(errordlg(message));
return;
end
% Open Excel as an ActiveX server.
objExcel = actxserver('Excel.Application');
objExcel.Visible = true;
% Open the workbook we want to paste the image onto.
ExcelWorkbook = objExcel.Workbooks.Open(fullFileName); % Full path is necessary!
% ExcelWorkbook = objExcel.Workbooks.Add(); % Add new, blank workbook.
oSheet = objExcel.ActiveSheet;
% oSheet.Range('A1').Select;
% Get the name of the image file.
imageFolder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
imageFullFileName = fullfile(imageFolder, 'cameraman.tif')
% Get a handle to Shapes for Sheet 1
Shapes = oSheet.Shapes;
% Add image by importing one from an image file on disk.
Shapes.AddPicture(imageFullFileName, 0, 1, 400, 18, 300, 235);
% Save the workbook.
% Tell it to not wait and pop up alerts like "This file exists. Do you want to overwrite it."
objExcel.DisplayAlerts = false;
% Save this workbook we just created to disk. Image will be saved with the workbook.
ExcelWorkbook.SaveAs(fullFileName);
% Close the workbook. Excel will still be open though.
ExcelWorkbook.Close(false);
objExcel.Quit; % Shut down Excel.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 ActiveX에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by