how to add image in determined excel cell in matlab??
8 views (last 30 days)
Show older comments
hi, i want add small image in determined cell using matlab
0 Comments
Answers (1)
Image Analyst
on 21 Feb 2015
What is a "determined" cell in Excel? How is that different than just a regular, normal cell? Anyway, cells hold numbers, not images so I'm not sure what you're thinking of. You can place an image onto a worksheet as a graphical element though, but you can't add it to anything.
4 Comments
Manoj Yasaswi
on 24 Jan 2018
Can you please post a sample code to display any image, for example, cameraman.tif in an excel cell using xlswrite() command.
Image Analyst
on 24 Jan 2018
See this snippet for how to paste an image onto an Excel Worksheet
%==========================================================================================================================
% Adapted from https://www.mathworks.com/matlabcentral/answers/91547-how-do-i-insert-a-matlab-figure-into-an-excel-workbook-through-activex
function PasteFigureIntoExcel(Excel, imageFullFileName)
try
% Use whatever sheet is active at the moment. This should be setup in advance of calling this function.
currentSheet = Excel.ActiveSheet;
% Get a handle to Shapes for Sheet 1
Shapes = currentSheet.Shapes;
% Add image by importing one from an image file on disk.
% Syntax:
% Shapes.AddPicture(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
% If width and height are -1, then it uses the native image dimensions.
Shapes.AddPicture(imageFullFileName, 0, 1, 350, 60, -1, -1);
% Delete the temporary image file.
% delete(imageFullFileName);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
WarnUser(errorMessage);
end
return;
You first need to get the Excel server as an activeX object and you do that like this:
try
% See if there is an existing instance of Excel running.
% If Excel is NOT running, this will throw an error and send us to the catch block below.
Excel = actxGetRunningServer('Excel.Application');
% If there was no error, then we were able to connect to it.
catch
% No instance of Excel is currently running. Create a new one.
% Normally you'll get here (because Excel is not usually running when you run this).
Excel = actxserver('Excel.Application');
end
invoke(Excel.Workbooks, 'Open', xlsFullFileName);
Excel.visible = true; % Make Excel appear so we can see it, otherwise it is hidden.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!