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
- MATLAB distinguishes between human-readable code (.m) and machine-optimized data (.mat).
- Compiled code (.mex) is generated for specific OS architectures, limiting cross-platform use.
- File manipulation functions (copyfile, movefile, delete) operate directly on the file system.
- Terminal interaction functions (unix, dos) map OS-specific command line interfaces to MATLAB commands.
Worked example
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?
- Use the movefile function to relocate and rename the file: movefile('experiment.mat', 'results/final_data.mat').
- Use the delete function to remove the file permanently: delete('temp.txt').
- 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.