simple example to write data into an excel file with actserver in matlab

Here is a simple example for writing data to an Excel file using actxserver function in MATLAB:

main.m
% Create an object of Excel
Excel = actxserver('Excel.Application');

% Make Excel visible
Excel.Visible = true;

% Add a new workbook
Workbook = Excel.Workbooks.Add();

% Select sheet 1
Sheet = Excel.ActiveSheet;

% Write data to cells A1 to B3
Sheet.range('A1').Value = 'Name';
Sheet.range('B1').Value = 'Age';
Sheet.range('A2').Value = 'John';
Sheet.range('B2').Value = 22;
Sheet.range('A3').Value = 'Jane';
Sheet.range('B3').Value = 25;

% Save the workbook
Workbook.SaveAs('example.xlsx');

% Close the workbook and Excel
Workbook.Close();
Excel.Quit();
559 chars
27 lines

This example creates an instance of Excel, adds a new workbook, writes data to cells A1 to B3, saves the workbook and then closes Excel.

Note: Make sure to close the workbook and Excel instance, else Excel will remain open and running in the background.

related categories

gistlibby LogSnag