Handling Files in MATLAB

1. MATLAB File Types:

  • .m files: MATLAB script and function files.

  • .mlx files: MATLAB live script files that combine code execution with narrative, formatted text, equations, images, and hyperlinks in a single, interactive document. Ideal for instructional purposes, interactive applications, and sharing results.

  • .mat files: Binary files that store workspace variables. Useful for saving your session data and transferring variables between sessions without losing integrity.

  • .fig files: Figure files that save MATLAB graphical outputs. They enable you to reopen and modify figures later, preserving the graphical data and properties exactly as saved.

2. Importing and Exporting Files

MATLAB supports importing and exporting various data formats such as TXT, CSV, XLS, XLSX, JPG, PNG, etc. You can find additional documentation including functions and examples here.

3. Commands for Handling Files

Command Description
save 'filename' Saves workspace variables to a file.
load 'filename' Loads variables from a file into the workspace.
readtable('filename') Imports data from a file into a table, supporting CSV, TXT, and Excel formats.
readmatrix('filename') Reads numerical data from a file into a matrix, ideal for straightforward data formats.
xlsread('filename') Imports data from Excel files, useful for datasets in spreadsheets.
csvread('filename') Reads numerical data from CSV files directly into an array.
fopen('filename') Opens a file for reading or writing, necessary for file manipulation.
fwrite(fid, data) Writes binary data to the file specified by fid.
fprintf(fid, format, data) Writes formatted data to the file, allowing for customizable text outputs.
fclose(fid) Closes an open file to free up resources.
importdata('filename') Loads mixed data from files, handling various formats automatically.
exportdata('data', 'filename') Exports data to a file in chosen formats, useful for sharing or external use.

4. Suggested Tutorials

Lesson: Gettting Data Into MATLAB

Tutorial: M Files

5. Supplemental Materials

Live Demo: Data Import

[Live Demo: Data Output] (https://www.tutorialspoint.com/matlab/matlab_data_import.htm)

Quick Guide: M Files

Documentation: Supported File Formats

Documentation: Spreadsheets

Documentation: Data Import and Export

Documentation: File Operations

Documentation: Cloud File Storage

Next Page
Next Page