2017-08-30 374 views
1

我跟在my own question后面。通过使用:在matlab中追加excel

excelWorkbook.SaveAs('figtest.xlsx'); 

我覆盖现有的Excel。我已经创建了一个函数,它使用代码来保存两个图像以达到最佳效果。现在,我想迭代大量图像,处理每个图像,然后将结果与原始图像一起保存为excel。显然每次迭代调用函数都是一个坏主意,因为每次迭代都会删除前一次迭代的结果。

如何在不删除以前的数据的情况下将当前数据添加到excel文件?

有没有更好的方法来做到这一点?

请注意,数据是图像而不是简单的数字数据。

回答

1

创建一个COM服务器一次,迭代图像并将它们放在所需的单元格中,退出服务器,然后删除服务器对象。

my previous answer大厦,这里是有许多内置的图像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 

这给:

output

0

刚刚发现这个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从文件交换。

+0

这将适用于简单的数字数据。我不确定这将适用于图像。我错过了什么吗? – havakok

0

将数据添加到excel文件没有很好定义:创建新工作表或将数据添加到现有工作表?

因此,您可能需要以下方法:

  1. 打开现有的Excel文件(您想添加数据)
  2. 新数据添加到打开的Excel文件
  3. 保存此编辑excel文件
1

一种方法是将每个图像集在自己的工作表中,根据需要在Excel文件中创建新工作表。以下是一些示例代码,修改自my previous answer

% 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(); 
+0

单元大小是否有限制?我得到'excelSheet.Range('B2')的Dispatch Exception。RowHeight = excelSheet.Shapes.Item(1).Height;'。我已经尝试过'excelSheet.Range('B2')。RowHeight = 409'并且没有错误,我在'excelSheet.Range('B2')收到同样的错误。RowHeight = 410' – havakok

+1

@havakok:是的,as (https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3)中,单元格的最大宽度限制为255个字符最高409点。 – gnovice