AI, machine learning and deep learning · Introductory

Exporting Text Files

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Exporting and importing text files involves reading data from formats like CSV and ASCII using tools such as Python's pandas or MATLAB's built-in functions, while managing delimiters and data types. Proper file handling requires managing file pointers, parsing strings into numerical arrays, and ensuring consistent naming conventions for reproducibility.

Intuition

Think of a text file as a raw stream of characters that needs to be organized into a structured format like a table or matrix. In Python, the pandas library acts as a smart parser that automatically handles this structure, allowing you to specify how columns are separated and what data types they hold. In MATLAB, the process is more manual, requiring you to open a 'pointer' to the file, read it line by line, and then manually split and convert each piece of text into numbers. Both methods aim to transform unstructured text into usable data structures for analysis, but they differ in automation level and syntax.

Derivation

  1. Identify the file format (CSV, ASCII) and its delimiter (comma, tab, space).
  2. In Python: Use pd.read_csv with filepath, sep, and dtype arguments to load data directly into a DataFrame.
  3. In MATLAB: Use fopen to get a file identifier (fid), then use fgetl in a loop until feof is true.
  4. Parse each line in MATLAB using regexp to split by delimiters into a cell array.
  5. Convert cell array strings to numbers using cellfun and str2double.
  6. Ensure consistent file naming to allow automated processing of multiple datasets.

Worked example

Problem

You have a text file named 'data.txt' with tab-separated values, including a header row. How would you import this in Python and MATLAB?

  1. Python: Use pd.read_csv('data.txt', sep='\t', header=0). This reads the file, uses tabs as delimiters, and treats the first row as column names.
  2. MATLAB: Use fid = fopen('data.txt'); to open the file. Then loop with while ~feof(fid), use fgetl(fid) to read lines, regexp to split by tabs, and cellfun(@str2double, ...) to convert to numbers.

Answer Python: df = pd.read_csv('data.txt', sep='\t', header=0). MATLAB: fid = fopen('data.txt'); line = fgetl(fid); parts = regexp(line, '\t', 'split'); nums = cellfun(@str2double, parts); fclose(fid);

Common mistakes

  • Assuming read_csv in Python only works for CSV files; it can actually read various text files with different delimiters.
  • Confusing the file identifier (fid) in MATLAB with a normal variable; it is actually a pointer to the file's location on the hard drive.
  • Forgetting that Python uses zero-based indexing, which can lead to off-by-one errors when slicing lists or arrays.
  • Assuming str2double and sscanf in MATLAB work directly on cell arrays; cellfun is required to apply these functions to each element of the cell array.
  • Thinking that the number of characters in a filename string is special; it is only relevant for string manipulation tasks like replacing parts of a filename.

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.