在matlab中添加excel
我正在跟进。通过使用:在matlab中添加excel,matlab,append,export-to-excel,Matlab,Append,Export To Excel,我正在跟进。通过使用: excelWorkbook.SaveAs('figtest.xlsx'); 我正在覆盖现有的excel。我创建了一个函数,它使用代码将两个图像保存到excel。现在,我想迭代许多图像,处理每个图像,然后将结果与原始图像一起保存到excel中。显然,每次迭代调用函数都是一个坏主意,因为每次迭代都会删除以前迭代的结果 如何将当前数据添加到excel文件而不删除任何以前的数据 有更好的方法吗 请注意,数据是图像,而不是简单的数字数据。刚刚发现: 此外,您可能希望尝试从文件交
excelWorkbook.SaveAs('figtest.xlsx');
我正在覆盖现有的excel。我创建了一个函数,它使用代码将两个图像保存到excel。现在,我想迭代许多图像,处理每个图像,然后将结果与原始图像一起保存到excel中。显然,每次迭代调用函数都是一个坏主意,因为每次迭代都会删除以前迭代的结果
如何将当前数据添加到excel文件而不删除任何以前的数据
有更好的方法吗
请注意,数据是图像,而不是简单的数字数据。刚刚发现:
此外,您可能希望尝试从文件交换中进行操作。向excel文件添加数据的定义不明确:是创建新工作表还是向现有工作表添加数据 因此,您可能需要以下方法:
创建一个COM服务器,遍历图像并将其放置在所需的单元格中,退出服务器,然后删除服务器对象 从中构建,以下是一个工作示例,其中有5个内置图像:
%Some sample images
im{1}=imread('peppers.png');
im{2}=imread('cameraman.tif');
im{3}=imread('pears.png');
im{4}=imread('greens.jpg');
im{5}=imread('bag.png');
excel = actxserver('Excel.Application'); % Create server object
excelWorkbook = excel.Workbooks.Add(1); % Add a workbook
excelSheet = excel.ActiveSheet; % Get the active sheet
f = figure('visible','off'); % To not show the images in MATLAB
dpi = get(groot, 'ScreenPixelsPerInch'); % Get screen dpi
for k=1:5
imshow(im{k});
print(gcf, sprintf('-r%d', dpi), ... % Print the figure at the screen resolution
'-clipboard', '-dbitmap'); % to the clipboard as a bitmap
%Adjust the cell names where you want to paste the images as desired
excelSheet.Range(['B',num2str(k)]).PasteSpecial();
%Unlocking aspect ratio to adjust height and width of the image according to the cell
excelSheet.Shapes.Item(k).LockAspectRatio='msoFalse';
excelSheet.Shapes.Item(k).Width=excelSheet.Range(['B',num2str(k)]).Width;
excelSheet.Shapes.Item(k).Height=excelSheet.Range(['B',num2str(k)]).Height;
end
excelWorkbook.SaveAs('figtest.xlsx'); % Save workbook to a file
excelWorkbook.Close(); % Close workbook
excel.Quit(); % Quit server
excel.delete(); % Delete server object
其中:
一种方法是将每组图像放在自己的工作表中,根据需要在Excel文件中创建新的工作表。以下是一些示例代码,修改自:
这将适用于简单的数字数据。我不确定这是否适用于图像。我遗漏了什么吗?单元格大小有限制吗?我收到excelSheet.Range('B2')的调度异常。行高=excelSheet.Shapes.Item(1).高度代码>。我尝试了
excelSheet.Range('B2')。行高=409
,但没有错误,我在excelSheet.Range('B2')。行高=410
@havakok:是的,如上所述,单元格的最大宽度限制为255个字符,最大高度限制为409个点。
%Some sample images
im{1}=imread('peppers.png');
im{2}=imread('cameraman.tif');
im{3}=imread('pears.png');
im{4}=imread('greens.jpg');
im{5}=imread('bag.png');
excel = actxserver('Excel.Application'); % Create server object
excelWorkbook = excel.Workbooks.Add(1); % Add a workbook
excelSheet = excel.ActiveSheet; % Get the active sheet
f = figure('visible','off'); % To not show the images in MATLAB
dpi = get(groot, 'ScreenPixelsPerInch'); % Get screen dpi
for k=1:5
imshow(im{k});
print(gcf, sprintf('-r%d', dpi), ... % Print the figure at the screen resolution
'-clipboard', '-dbitmap'); % to the clipboard as a bitmap
%Adjust the cell names where you want to paste the images as desired
excelSheet.Range(['B',num2str(k)]).PasteSpecial();
%Unlocking aspect ratio to adjust height and width of the image according to the cell
excelSheet.Shapes.Item(k).LockAspectRatio='msoFalse';
excelSheet.Shapes.Item(k).Width=excelSheet.Range(['B',num2str(k)]).Width;
excelSheet.Shapes.Item(k).Height=excelSheet.Range(['B',num2str(k)]).Height;
end
excelWorkbook.SaveAs('figtest.xlsx'); % Save workbook to a file
excelWorkbook.Close(); % Close workbook
excel.Quit(); % Quit server
excel.delete(); % Delete server object
% Start COM server:
excel = actxserver('Excel.Application');
excelWorkbook = excel.Workbooks.Add(1);
excelWorksheets = excelWorkbook.Worksheets;
% Initializations:
dpi = get(groot, 'ScreenPixelsPerInch');
for iSheet = 1:nIterations % Based on how many image sets you process
% Get a blank sheet:
if (iSheet == 1)
excelSheet = excel.ActiveSheet;
else
excelSheet = excelWorksheets.Add([], excel.ActiveSheet, 1);
end
% Load, process, and plot your images here:
...
% Paste image into sheet and adjust cell size:
print(hFigure, sprintf('-r%d', dpi), '-clipboard', '-dbitmap');
excelSheet.Range('B2').PasteSpecial();
excelSheet.Range('B2').RowHeight = excelSheet.Shapes.Item(1).Height;
widthScale = excelSheet.Range('B2').ColumnWidth./...
excelSheet.Range('B2').Width;
excelSheet.Range('B2').ColumnWidth = excelSheet.Shapes.Item(1).Width.*widthScale;
end
% Save and close workbook and stop COM server:
excelWorkbook.SaveAs('figtest.xlsx');
excelWorkbook.Close();
excel.Quit();
excel.delete();