How to Write Data to Excel Spreadsheets in MATLAB?


MATLAB provides various built-in functions that enable us to write data in the form of tables, matrices, cell arrays, etc. to an Excel file. Read this article to learn the process of writing data to Excel spreadsheets in MATLAB.

The following are some commonly used methods to write data to excel spreadsheets using MATLAB −

  • Using the "writetable" Function

  • Using the "xlswrite" Function

  • Using the "writematrix" Function

  • Using the "writecell" Function

Let us discuss each of these methods in detail with the help of examples.

Using "writetable" Function in MATLAB

In MATLAB, the "writetable" is a built-in function that can be used to write tabular data to an excel spreadsheet. This function is a recommendation for writing data to an excel spreadsheet in newer versions of MATLAB.

Here is its syntax

writetable(data_table, excel_file_name);

Here, the "data_table" is a variable containing actual data in tabular form.

Example

Let us take an example to understand how to use the "writetable" function to write data to an excel spreadsheet.

% MATLAB code to write data to excel spreadsheet using writetable function
% Create some sample data
data = randn(4, 3);		% generating random matrix of data

% Create column headers
col_headers = {'Col1', 'Col2', 'Col3'};

% Convert the data to a data table format
data_table = array2table(data, 'VariableNames', col_headers);

% Specify a file name for excel spreadsheet
file_name = 'excel_file_1.xlsx';

% Write the data table to the Excel spreadsheet
writetable(data_table, file_name);

% Display a conformation message
disp('Data has been written to the excel spreadsheet successfully.');

Output

It will produce the following output −

Data has been written to the excel spreadsheet successfully.

Using "xlswrite" Function in MATLAB

In MATLAB, we have another built-in function named "xlswrite" to write data to an excel spreadsheet. This function is an older method of writing data to an excel spreadsheet in MATLAB.

This function has some limitations over the "writetable" function. Hence, if you are using a newer version of MATLAB, then it is recommended to use the "writetable" function instead of the "xlswrite" function.

Here is its syntax

xlswrite(file_name, data, sheet_name);

Example

Let us see an example to understand the use of the "xlswrite" function to write data to a spreadsheet.

% MATLAB code to write data to excel spreadsheet using "xlswrite" function
% Create sample data
data = randn(5, 6);  	% generating random data

% Specify the name of the Excel file
file_name = 'excel_file_2.xlsx';

% Specify the name for spreadsheet
sheet_name = 'Sample_Sheet';

% Write the data to the Excel spreadsheet
xlswrite(file_name, data, sheet_name);

% Show a conformation message
disp('Data has been written to the excel spreadsheet successfully.');

Output

It will produce the following output −

Data has been written to the excel spreadsheet successfully.

Using "writematrix" Function in MATLAB

In MATLAB, the "writematrix" function is used to write matrices to an excel spreadsheet. This function is also recommended to use in the newer versions of MATLAB.

Its syntax is as follows −

writematrix(mat, file_name);

Example

The following example demonstrates use of the "writematrix" function to write matrix data to an excel spreadsheet.

% MATLAB code to write data to excel spreadsheet using "writematrix" function
% Create a sample matrix of data
mat = randn(5, 4);	% generating a random matrix

% Specify the file name for excel spreadsheet
file_name = 'excel_file_3.xlsx';

% Write the matrix data to the Excel spreadsheet
writematrix(mat, file_name);

% Show a conformation message
disp('Data has been written to the excel spreadsheet successfully.');

Output

It will produce the following output −

Data has been written to the excel spreadsheet successfully.

Using "writematrix" Function in MATLAB

In MATLAB, the "writecell" function is used to write cell array data to an excel spreadsheet. A cell array contains elements of different data types. The "writecell" function provides an efficient way to handle different types of data.

The "writecell" function is available in MATLAB R2019a and later versions of MATLAB. In the older versions of MATLAB, we have to use the "xlswrite" function to write the data of a cell array to an excel spreadsheet.

Here is its syntax

writecell(cell_array, file_name);

Example

Let us take an example to understand the use of the "writecell" function to write a cell array to an excel spreadsheet.

% MATLAB code to write data to excel spreadsheet using writecell function
% Create an example cell array
A = {'ID', 'Course', 'Fee'; 1001, 'MATLAB', 250; 1002, 'Python', 450; 1003, 'HTML', 350; 1004, 'JavaScript', 400;};

% Specify the file name for excel spreadsheet
file_name = 'excel_file_4.xlsx';

% Write the cell array to the Excel spreadsheet
writecell(A, file_name);

% Show a conformation message
disp('Data has been written to the excel spreadsheet successfully.');

Output

It will produce the following output −

Data has been written to the excel spreadsheet successfully.

Conclusion

In conclusion, MATLAB provides various methods to write data to an excel spreadsheet. In this tutorial, I explained all the commonly used methods to write data to an example using MATLAB. The examples demonstrate the use of different function to write different kinds of data to an excel spreadsheet in MATLAB.

Updated on: 25-Oct-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements