0

I have the following MATLAB code:

for i=1:3
    a=rand(5,1);
    b=rand(5,1);
    plot(a,b);
    xlabel('X Values');
    ylabel('Y Values');

    xlswritefig(gcf, 'Results.xlsx', 'Sheet1', 'D2') 
    % from https://www.mathworks.com/matlabcentral/fileexchange/24424-xlswritefig

end

This generates just the 3rd plot in an Excel file. However, I want to generate all 3 plots, side-by-side, in a single Excel file. How can I do this?

EDIT:

I modified the code to use actxserver instead of xlswritefig. However, as the code runs, it generates 3 excel pop-ups showing the plots. However, the file results1.xlsx contains no plots. I don't even want peaks in the code, but if I exclude it, nothing even shows up in the Excel pop-ups. Here's the new code:

cellPos = {'D2', 'J2', 'P2'};

for i=1:3
    a=rand(5,1);
    b=rand(5,1);
    plot(a,b);
    xlabel('X Values');
    ylabel('Y Values');

    f1 = figure; peaks;

    % Connect to Excel, make it visible and add a worksheet
    xl = actxserver('Excel.Application'); set(xl,'Visible',1);
    xl.Workbooks.Add(1); xls = xl.ActiveSheet;

    % Paste in the MATLAB figures
    print(f1, '-dbitmap'); xls.Range(cellPos{i}).PasteSpecial;
    xls.Shapes.Item(1).PictureFormat.CropLeft  = 30;
    xls.Shapes.Item(1).PictureFormat.CropRight  = 30;
    xls.Shapes.Item(1).Height  = 100;
    xls.Shapes.Item(1).Left = xls.Range(cellPos{i}).Left;
    excel = actxserver('Excel.Application');  % Create server object
    excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
    excelWorkbook.SaveAs('results1.xlsx');  % Save workbook to a file
    excelWorkbook.Close();                 % Close workbook
    excel.Quit();                          % Quit server
    excel.delete();                        % Delete server object     


end
gnovice
  • 125,304
  • 15
  • 256
  • 359
aaa
  • 39
  • 1
  • 8
  • You're writing all the figures to the same cell `D2`. Write them to different cells – Sardar Usama Dec 11 '17 at 19:53
  • That worked. However, now all the plots are a default size. How can I resize them so that they are a size specified by the user? I provided an updated code in the original question – aaa Dec 11 '17 at 20:21
  • @aaa: The [`xlswritefig`](https://github.com/michellehirsch/xlswritefig/blob/master/xlswritefig.m) function uses [`hgexport`](https://www.mathworks.com/help/matlab/ref/hgexport.html) to copy the figure to the clipboard. That seems to be limiting your control of the figure size. The answers I linked to below use [`print`](https://www.mathworks.com/help/matlab/ref/print.html), which gives you more options. You may want to use the code from those answers instead of `xlswritefig`. – gnovice Dec 11 '17 at 20:38

1 Answers1

0

You're writing all the images to the same sheet and cell 'D2'. You need to change the cell on each loop iteration so each figure is in a different place instead of one on top of the other. For example, this places a figure in every third cell horizontally:

cellPos = {'D2', 'G2', 'J2'};
for i = 1:3
  ...
  xlswritefig(gcf, 'Results.xlsx', 'Sheet1', cellPos{i});
end

You may also want to have a look at the answers to this other question, which detail how to create an image from a figure and insert that image into an Excel cell, resizing the image or cell as needed.

Edit:

Based on your edit (where you are now using some code from the link I gave above) you have quite a few errors. You only need to start the COM server once before your loop, and then close it after your loop. You also need to create your figure window f1 before you plot your data. Here's a rewrite using the example code here (with cell modifications omitted):

cellPos = {'D2', 'J2', 'P2'};             % Cells to place figures in
dpi = get(groot, 'ScreenPixelsPerInch');  % Get screen dpi
excel = actxserver('Excel.Application');  % Create server object
excelWorkbook = excel.Workbooks.Add(1);   % Add a workbook
excelSheet = excel.ActiveSheet;           % Get the active sheet
for i = 1:3
    a = rand(5, 1);
    b = rand(5, 1);
    f1 = figure;
    plot(a, b);
    xlabel('X Values');
    ylabel('Y Values');
    print(f1, sprintf('-r%d', dpi), ...  % Print the figure at the screen resolution
          '-clipboard', '-dbitmap');     %   to the clipboard as a bitmap
    excelSheet.Range(cellPos{i}).PasteSpecial();  % Paste from clipboard
    % Modifications to cell size, etc.
end
excelWorkbook.SaveAs('Results.xlsx');  % Save workbook to a file
excelWorkbook.Close();                 % Close workbook
excel.Quit();                          % Quit server
excel.delete();                        % Delete server object     
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • I looked at the code from that link. I updated the code I now use in my original post and the errors I'm getting – aaa Dec 11 '17 at 20:46
  • that updated code works better, but `results1.xlsx` still contains no plots in it – aaa Dec 11 '17 at 21:02