附加到 matlab 中的 excel
Appending to an excel in matlab
我正在跟进 。通过使用:
excelWorkbook.SaveAs('figtest.xlsx');
我正在覆盖现有的 excel。我创建了一个函数,它使用代码将两个图像保存到 excel。现在我想遍历许多图像,处理每个图像,然后将结果与原始图像一起保存到 excel。显然每次迭代都调用该函数是一个坏主意,因为每次迭代都会删除前一次迭代的结果。
如何将当前数据添加到 excel 文件而不删除任何以前的数据?
有更好的方法吗?
请注意,数据是图像而不是简单的数字数据。
刚找到这个 here:
xlApp = actxserver('Excel.Application');
xlApp.visible = 1;
%Open the the spreadsheet
xlworkbook = xlApp.Workbooks.Open('figtest.xlsx');
xlsheet = xlworkbook.ActiveSheet;
mydata=randn(1,3);
data=xlsread('figtest.xlsx');
%Determine last row
last=size(data,1);
newRange=last+1;
xlCurrRange = xlsheet.Range(['A', num2str(newRange),':C', num2str(newRange)]);
xlCurrRange.Value2 = mydata;
%Save and Close the Excel File
invoke(xlworkbook,'Save');
invoke(excelApp,'Quit');
delete(excelApp);
此外,您可能想尝试 this script 从文件交换。
向 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
给出:
一种方法是将每组图像放在自己的 sheet 中,根据需要在 Excel 文件中创建新的 sheet。这是一些示例代码,修改自 :
% 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();
我正在跟进
excelWorkbook.SaveAs('figtest.xlsx');
我正在覆盖现有的 excel。我创建了一个函数,它使用代码将两个图像保存到 excel。现在我想遍历许多图像,处理每个图像,然后将结果与原始图像一起保存到 excel。显然每次迭代都调用该函数是一个坏主意,因为每次迭代都会删除前一次迭代的结果。
如何将当前数据添加到 excel 文件而不删除任何以前的数据?
有更好的方法吗?
请注意,数据是图像而不是简单的数字数据。
刚找到这个 here:
xlApp = actxserver('Excel.Application');
xlApp.visible = 1;
%Open the the spreadsheet
xlworkbook = xlApp.Workbooks.Open('figtest.xlsx');
xlsheet = xlworkbook.ActiveSheet;
mydata=randn(1,3);
data=xlsread('figtest.xlsx');
%Determine last row
last=size(data,1);
newRange=last+1;
xlCurrRange = xlsheet.Range(['A', num2str(newRange),':C', num2str(newRange)]);
xlCurrRange.Value2 = mydata;
%Save and Close the Excel File
invoke(xlworkbook,'Save');
invoke(excelApp,'Quit');
delete(excelApp);
此外,您可能想尝试 this script 从文件交换。
向 excel 文件添加数据未明确定义:创建新工作表或向现有工作表添加数据?
因此,您可能需要以下方法:
- 打开现有的 excel 文件(您要向其添加数据)
- 将新数据添加到打开的 excel 文件中
- 保存这个已编辑的 excel 文件
创建 COM 服务器一次,遍历图像并将它们放在所需的单元格中,退出服务器,然后删除服务器对象。
从
%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
给出:
一种方法是将每组图像放在自己的 sheet 中,根据需要在 Excel 文件中创建新的 sheet。这是一些示例代码,修改自
% 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();