Skip to content

MATLAB - Data Output

Exporting data from MATLAB means writing variables from the workspace into files. This is essential for saving results, sharing data with other applications, or creating reports. Modern MATLAB offers a suite of high-level functions that make data export simple and robust.

Section titled “Modern High-Level I/O Functions (Recommended)”

For most common data export tasks, you should use the write___ family of functions. These functions are powerful, flexible, and have replaced older functions like dlmwrite.

  • writematrix: Writes a numeric matrix to a text file.
  • writecell: Writes a cell array to a text file.
  • writetable: Writes a table to a file. This is the most powerful option for mixed-type tabular data (e.g., columns of numbers, text, and dates) and supports formats like .csv and .xlsx.

Let’s create a numeric matrix and save it to a comma-separated values (CSV) file.

% Create a sample numeric matrix
num_matrix = [1.5, 2.3, 8.1; 4.0, 5.2, 6.9];
% Export the matrix to a CSV file
output_filename = 'matrix_data.csv';
writematrix(num_matrix, output_filename);
% Verify by displaying the file content
fprintf('File ''%s'' created. Content:\n', output_filename);
type(output_filename);

The output file matrix_data.csv will contain:

1.5,2.3,8.1
4,5.2,6.9

The table data type is excellent for handling data with named columns of different types. writetable automatically includes the column headers in the output file.

% Create a table with mixed data types
LastName = ["Smith"; "Jones"; "Williams"];
Age = [38; 29; 45];
BloodPressure = [124, 82; 115, 78; 130, 88];
data_table = table(LastName, Age, BloodPressure);
% Export the table to a text file with a tab delimiter
output_filename = 'patient_data.txt';
writetable(data_table, output_filename, 'Delimiter', '\t');
% Verify by displaying the file content
fprintf('\nFile ''%s'' created. Content:\n', output_filename);
type(output_filename);

The output file patient_data.txt will be neatly formatted:

LastName Age BloodPressure_1 BloodPressure_2
"Smith" 38 124 82
"Jones" 29 115 78
"Williams" 45 130 88

When you need complete control over the file format (e.g., creating a structured log file or a custom text format), you can use the low-level I/O functions: fopen, fprintf, and fclose.

This example creates a two-column table in a text file, demonstrating robust error handling and file management.

% Prepare data
x = 0:10:100;
y_data = [x; log(x)];
output_filename = 'log_table.txt';
% Open file for writing ('w'). Best practice is to check the file ID.
fid = fopen(output_filename, 'w');
if fid == -1
error('Cannot open file: %s', output_filename);
end
% Use a try-catch block to ensure the file is closed even if errors occur
try
% Write a header to the file
fprintf(fid, '--- Logarithm Table ---\n');
fprintf(fid, '%-10s | %-15s\n', 'Value', 'Log(Value)');
fprintf(fid, '---------------------------\n');
% Write the data in a loop for precise formatting
fprintf(fid, '%-10.2f | %-15.4f\n', y_data);
fprintf('\nFile ''%s'' created successfully.\n', output_filename);
catch ME
% If an error occurs during writing, close the file and rethrow the error
fclose(fid);
rethrow(ME);
end
% Close the file
fclose(fid);
% Display the created file
type(output_filename);

The resulting file log_table.txt will look like this:

--- Logarithm Table ---
Value | Log(Value)
---------------------------
0.00 | -Inf
10.00 | 2.3026
20.00 | 2.9957
30.00 | 3.4012
40.00 | 3.6889
50.00 | 3.9120
60.00 | 4.0943
70.00 | 4.2485
80.00 | 4.3820
90.00 | 4.4998
100.00 | 4.6052
  • diary: Creates a log of your MATLAB command window session. Useful for simple record-keeping. Use diary on to start, diary off to stop.
  • save command: Primarily used for saving workspace variables to a .mat file (save('my_data.mat')), which is a binary format perfect for reloading into MATLAB later. The -ascii option is considered legacy and less flexible than writematrix.

Note on Deprecated Functions: The function dlmwrite is no longer recommended. Use writematrix for its improved performance and features.