Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

matlab_convert_mat2csv.m - Convert MATLAB .mat files to CSV format

Description

This script automatically converts all MATLAB data files (.mat) in the current working directory to CSV format. It extracts all variables from each .mat file and saves them as separate CSV files.

Usage

From within MATLAB

>> matlab_convert_mat2csv

From command line

matlab -batch "cd('path/to/mat/files'); matlab_convert_mat2csv"

Input

Output

Behavior

Dependencies

Limitations

Important Note

⚠️ Warning: This script will exit MATLAB upon completion. Use with caution in interactive sessions.

How It Works

  1. File Discovery: Scans current directory for all .mat files

  2. File Processing: For each .mat file:

    • Loads the file using load() function

    • Extracts all variable names using fieldnames()

    • For each variable, creates a CSV file named after the variable

    • Uses writematrix() to write numeric data to CSV

  3. Cleanup: Automatically exits MATLAB when all conversions are complete

Example Workflow

# Navigate to directory with .mat files
cd /path/to/data

# Run conversion (will exit MATLAB when done)
matlab -batch "matlab_convert_mat2csv"

# Results: each variable in each .mat file becomes a separate .csv file

Example Output

If you have a file experiment.mat containing:

The script will create:

This tool is useful for converting MATLAB data to a more portable CSV format for analysis in other tools or languages.