Lesson 07-File I_O Operations and External Data Import_Export

Introduction: The Data Pipeline Problem

If you’ve been following this MATLAB course series, you’ve learned how to manipulate data in memory — matrices, strings, cell arrays, structures, and tables. But here’s the uncomfortable truth: data doesn’t magically appear in your MATLAB workspace. And when you’ve spent hours analyzing that data, you’d better have a way to save your results, or all that work vanishes when you close MATLAB.

File I/O (Input/Output) is the unglamorous plumbing of data science. It’s not as intellectually stimulating as designing algorithms or visualizing data, but it’s absolutely critical. In the real world, data lives in files: CSV files from sensors, MAT-files from previous analyses, images from cameras, audio from microphones, and specialized formats like HDF5 for scientific computing.

In this article, we’re going to explore the full spectrum of MATLAB’s file I/O capabilities. We’ll start with low-level file operations (the building blocks), move through specialized functions for common formats, and finish with cutting-edge techniques for working with datasets too large to fit in memory. By the end, you’ll know not just how to read and write files, but how to make intelligent choices about file formats, understand the performance implications of different approaches, and handle the messy realities of real-world data.

The Philosophy of MATLAB File I/O

Before diving into syntax, let’s talk about philosophy. MATLAB’s file I/O system has evolved significantly over the decades, and understanding this evolution helps you navigate the sometimes-bewildering array of functions.

In the early days (think 1980s and 1990s), MATLAB provided low-level file I/O functions modeled after C’s stdio library: fopen, fread, fwrite, fprintf, fscanf, fclose. These functions give you fine-grained control but require you to manage everything manually. They’re still available and still useful for specific tasks.

As MATLAB matured, MathWorks added higher-level functions tailored to specific file formats: imread/imwrite for images, audioread/audiowrite for audio, readtable/writetable for tabular data. These functions handle the messy details (file headers, encoding, compression) so you don’t have to.

In recent years, MATLAB has pivoted toward a “just read it” philosophy with functions like readtable, readmatrix, and readlines. These functions use sophisticated autodetection to figure out the file format and deliver the data in a useful format. They’re the easiest to use, but they’re not always the most efficient for very large files.

The result of this evolution is that MATLAB now has three “layers” of file I/O:

  1. Low-level functions: Maximum control, maximum complexity
  2. Format-specific functions: Good balance of control and convenience
  3. Universal reading functions: Maximum convenience, less control

A skilled MATLAB programmer knows when to use each layer. Let’s explore them all.

Low-Level File I/O: The Building Blocks

Low-level file I/O in MATLAB mirrors C’s stdio library. If you’ve done systems programming in C, this will feel familiar. If not, don’t worry — I’ll explain everything.

Opening and Closing Files: fopen and fclose

Every file operation in MATLAB starts with fopen and ends with fclose. Think of fopen as “getting a ticket to access the file” and fclose as “returning the ticket.”

% Open a file for reading
fid = fopen('data.txt', 'r');
if fid == -1
    error('Could not open file for reading');
end
% ... do stuff with the file ...
fclose(fid);

% Open a file for writing (creates or overwrites)
fid = fopen('output.txt', 'w');
if fid == -1
    error('Could not open file for writing');
end
% ... write stuff ...
fclose(fid);

The fopen function returns a file identifier (fid) — a number that MATLAB uses to track the file. If fopen returns -1, something went wrong (file doesn’t exist, permission denied, etc.).

Critical point: Always check the return value of fopen, and always call fclose when you’re done. If you don’t close a file, MATLAB holds onto the file handle, and you might not be able to reopen the file or delete it until you restart MATLAB.

The second argument to fopen is the mode string. Here are the most common modes:

ModeMeaning
'r'Open for reading (default)
'w'Open for writing (creates new or overwrites)
'a'Open for appending (creates new or adds to end)
'r+'Open for reading and writing
'w+'Open for reading and writing (creates new or overwrites)
'a+'Open for reading and writing (appends)

You can also add modifiers:

  • 'b' for binary mode (e.g., 'rb', 'wb')
  • 'n' for “no automatic flushing” (advanced use)
  • 'A' for “no automatic byte swapping” (for network byte order)
% Binary file for reading
fid = fopen('data.bin', 'rb');

% Text file for writing (explicit text mode)
fid = fopen('output.txt', 'wt');

Reading and Writing Text: fscanf and fprintf

Once you have a file open, you can read and write formatted text with fscanf and fprintf. These functions work like C’s scanf and printf, using format specifiers to describe the data.

% Write formatted text
fid = fopen('output.txt', 'w');
fprintf(fid, 'Name: %s, Age: %d, Score: %.2f\n', 'Alice', 32, 95.5);
fprintf(fid, 'Data: %d %d %d\n', [1, 2, 3]);
fclose(fid);

% Read formatted text
fid = fopen('output.txt', 'r');
% Read all name-value pairs
data = fscanf(fid, 'Name: %s, Age: %d, Score: %f\n');
fclose(fid);

Wait — that fscanf example has a problem. fscanf reads all matching data into a single array, which doesn’t work well for mixed-type data (strings and numbers). For that, you need a different approach:

% Better approach for mixed-type data: read line by line
fid = fopen('output.txt', 'r');
while ~feof(fid)  % feof checks for "end of file"
    line = fgetl(fid);  % Reads one line (strips newline)
    % Parse the line...
    if contains(line, 'Name:')
        % Extract data using regexp or split
    end
end
fclose(fid);

The format specifiers for fprintf and fscanf deserve their own table:

SpecifierMeaningExample
%dInteger42
%fFloating-point3.14159
%eScientific notation3.141593e+00
%gCompact floating-point3.14159
%sStringhello
%cCharacterh
%[^\n]Read until newline(entire line)
% Writing a matrix to a text file
A = magic(5);
fid = fopen('matrix.txt', 'w');
for i = 1:size(A, 1)
    fprintf(fid, '%d %d %d %d %d\n', A(i, :));
end
fclose(fid);

Binary I/O: fread and fwrite

When performance matters and you’re working with numeric data, binary I/O is much faster than text I/O. Binary files are also more compact. The trade-off is that they’re not human-readable.

% Write binary data
data = rand(1000, 1);
fid = fopen('data.bin', 'wb');
fwrite(fid, data, 'double');  % Write as 64-bit doubles
fclose(fid);

% Read binary data
fid = fopen('data.bin', 'rb');
data_loaded = fread(fid, [1000, 1], 'double');
fclose(fid);

The third argument to fread and fwrite is the precision string. It tells MATLAB how to interpret the bytes. Common precisions include:

  • 'double' — 64-bit floating-point (8 bytes per element)
  • 'single' — 32-bit floating-point (4 bytes per element)
  • 'int32' — 32-bit integer (4 bytes per element)
  • 'uint8' — 8-bit unsigned integer (1 byte per element)
  • 'char' — character (1-4 bytes per element, depending on encoding)
% Write mixed-type binary data
fid = fopen('mixed.bin', 'wb');
fwrite(fid, 42, 'int32');           % Write an integer
fwrite(fid, 3.14159, 'double');     % Write a double
fwrite(fid, 'Hello', 'char');        % Write a string
fclose(fid);

% Read it back (you must know the exact format!)
fid = fopen('mixed.bin', 'rb');
my_int = fread(fid, 1, 'int32');
my_double = fread(fid, 1, 'double');
my_string = fread(fid, 5, 'char')';
fclose(fid);
my_string = char(my_string);  % Convert from numeric to char

The danger of binary I/O: You need to know exactly what’s in the file to read it correctly. There’s no header that says “this file contains an int32, then a double, then 5 chars.” If you get the format wrong, you’ll get garbage data or an error. This is why binary formats usually have carefully designed headers.

File Positioning: fseek, ftell, frewind

When you read or write a file, MATLAB maintains a file position indicator — think of it as a cursor that keeps track of where you are in the file. You can move this cursor manually:

fid = fopen('data.bin', 'rb');

% Read the first 10 doubles
data1 = fread(fid, 10, 'double');

% Where are we now?
position = ftell(fid);  % Returns byte offset from start
fprintf('Current position: %d bytes from start\n', position);

% Jump to byte 100 from the start
fseek(fid, 100, 'bof');  % 'bof' = beginning of file

% Jump 50 bytes forward
fseek(fid, 50, 'cof');  % 'cof' = current position

% Jump to the end
fseek(fid, 0, 'eof');  % 'eof' = end of file

% Go back to the beginning
frewind(fid);
% (Equivalent to fseek(fid, 0, 'bof'))

fclose(fid);

File positioning is useful when you’re working with complex binary formats that have headers, indices, or other structural elements. You can jump directly to the part of the file you need instead of reading everything sequentially.

When to Use Low-Level I/O (and When Not To)

Low-level I/O gives you maximum control, but it comes at a cost: complexity. You have to handle everything yourself — file opening, error checking, data conversion, file closing. It’s easy to make mistakes.

Use low-level I/O when:

  • You’re working with a custom binary format
  • You need to read specific bytes from specific positions
  • You’re reading a very large file incrementally (not all at once)
  • No higher-level function exists for your file format

Don’t use low-level I/O when:

  • A higher-level function exists (readtable, imread, etc.)
  • You’re working with standard formats (CSV, images, audio)
  • You don’t have strict performance requirements
  • You value your time and sanity

For 95% of use cases, you should be using higher-level functions. Let’s talk about those.

Membership Required

You must be a member to access this content.

View Membership Levels

Already a member? Log in here

Share your love