Writing a Table to Excel Spreadsheet
Firstly, we shall create a table and then write the same to an excel spreadsheet with the help of writetable function.
Example 1:
Matlab
% MATLAB Code tab = magic(5); tab = array2table(tab, "VariableNames" , [ "R1" "R2" "R3" "R4" "R5" ]); disp(tab) % Writing the table to excel file writetable(tab, 'new.xls' , 'FileType' , 'spreadsheet' ) |
The output of the above code will create a new excel sheet in the current folder.
In the above code, we create a table from magic with the variable names or table headers passed as a vector. Then, in the writetable function, we pass the table, and the file name to be used (if present then, it’ll overwrite the data. If not, then it will create a new file and then, it’ll create a new file). The next argument is a field type that decides the file type and the argument following it is the value for the same field; spreadsheet in this case.
How to Write Data to Excel Spreadsheets in MATLAB?
MATLAB provides options to write a table, array, or matrix to Microsoft Excel spreadsheets. The function available to do so is the writetable () function. The general syntax for this function is:
Syntax:
writetable(<data>, <filename>, <optional_values>)
Now, in the following sections, we shall see how to write a table, an array, and a matrix into a spreadsheet.