2017-08-30 369 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