AI, machine learning and deep learning · Introductory

Interacting with Your Operating System via MATLAB

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

MATLAB utilizes specific native file formats like.m,.mat, and.fig for code, data, and graphics, while.mex files provide OS-specific compiled performance. Users interact with the operating system through file manipulation commands and by sending terminal commands via unix or dos functions.

Intuition

Think of MATLAB's file system as a structured workspace where different file extensions serve distinct purposes to optimize storage and execution. Plain text code lives in.m files, while compressed, unreadable binary data is stored in.mat files for efficiency. When you need to interact with the computer itself, MATLAB acts as a bridge, allowing you to move, copy, or delete files directly, or even execute external programs by sending commands to the system terminal, though you must remember that compiled.mex files are tied to specific operating systems and bit-versions.

Derivation

  1. MATLAB distinguishes between human-readable code (.m) and machine-optimized data (.mat).
  2. Compiled code (.mex) is generated for specific OS architectures, limiting cross-platform use.
  3. File manipulation functions (copyfile, movefile, delete) operate directly on the file system.
  4. Terminal interaction functions (unix, dos) map OS-specific command line interfaces to MATLAB commands.

Worked example

Problem

A researcher needs to move a data file named 'experiment.mat' from the current directory to a folder named 'results' and rename it 'final_data.mat'. They also need to delete an old file 'temp.txt' permanently. How should they use MATLAB commands?

  1. Use the movefile function to relocate and rename the file: movefile('experiment.mat', 'results/final_data.mat').
  2. Use the delete function to remove the file permanently: delete('temp.txt').
  3. Note that delete does not send the file to a trashcan, so the action is irreversible.

Answer movefile('experiment.mat', 'results/final_data.mat') and delete('temp.txt')

Common mistakes

  • Assuming the delete function moves files to a trashcan; it permanently removes them irretrievably.
  • Believing.mex files are cross-platform; they are specific to the operating system and bit-version (e.g., Mac vs. Windows, 32-bit vs. 64-bit).
  • Saving data in manufacturer-specific formats which may require specific licenses and hinder data sharing.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.