9.6: Reading Mixed Data with textscan

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    9.6: Reading Mixed Data with textscan

    The function textscan is useful when a text file contains mixed data, meaning the file includes more than one type of information. For example, one column may contain names, another column may contain numbers, and another column may contain letter grades or category labels.

    The general format is:

    data = textscan(fid, formatSpec)

    Here, fid is the file identifier returned by fopen, and formatSpec tells MATLAB what kind of data to expect in each column.

    For example, suppose a file named scores.txt contains:

    Chris 92
    Dana 84
    Lily 77
    

    The first column is text and the second column is numeric. We can read the file using:

    fid = fopen('scores.txt');
    data = textscan(fid, '%s %f');
    fclose(fid);
    

    The %s tells MATLAB to read the first column as text, and %f tells MATLAB to read the second column as floating-point numbers.

    The output from textscan is a cell array. Each cell contains one column of data.

    data{1} contains the names.

    data{2} contains the scores.

    So we can write:

    names = data{1};
    scores = data{2};
    

    This is one reason cell arrays are important. A regular numeric matrix cannot store both text and numbers, but a cell array can store different types of data together.

    Common Format Specifiers

    The format specification tells MATLAB how to interpret each column in the file.

    Format Specifier Meaning Example Data
    %f Floating-point number 92.5
    %d Integer 12
    %s Text with no spaces Chris
    %c Single character A
    %q Quoted text "San Diego"

    The most common format specifiers for beginner file I/O are %s, %f, and %c.

    Example \(\PageIndex{1}\)

    Suppose a file named example.txt contains a numeric score and a letter grade on each line:

    92 A 
    84 B 
    77 C 
    65 D

    We can read the first column as numbers and the second column as characters.

    fid = fopen('example.txt');
    if fid == -1
        disp('File open was not successful!')
    else
        myArray = textscan(fid, '%f %c');
        scores = myArray{1};
        letterGrades = myArray{2};
        cid = fclose(fid);
            if cid == -1
            disp('File close was not successful!')
        end
    end
    

    The variable myArray is a cell array with two cells. The first cell contains the numeric scores, and the second cell contains the letter grades.

    Notice the use of curly braces: myArray{1} and myArray{2}. This extracts the contents of the cells. If we used parentheses, such as myArray(1), MATLAB would return a smaller cell array instead of the actual data.

    Skipping Header Lines

    Many real data files include a header line. For example:

    LastName FirstName Weight
    Grey Chris 166.2
    Hall David 143.5
    Giovanni Luke 192.4
    Cat Donna 215.1
    

    The first line contains labels, not actual data. To skip this line, use the 'HeaderLines' option:

    patientData = textscan(fid, '%s %s %f', 'HeaderLines', 1);

    This tells MATLAB to ignore the first line before reading the data.

    Handling Delimiters

    A delimiter is the character that separates values in a file. Some files use spaces, while others use commas or tabs.

    For example, this file uses commas:

    Grey,Chris,166.2
    Hall,David,143.5
    

    To read a comma-separated file, specify the delimiter:

    patientData = textscan(fid, '%s %s %f', 'Delimiter', ',');

    This is useful when reading CSV-style text files.

    Common Beginner Mistakes with textscan

    A common mistake is using the wrong format specifier. For example, if a column contains decimal values, use %f, not %d.

    Another common mistake is forgetting that textscan returns a cell array. Use curly braces to extract the actual contents:

    patientData{1}

    not:

    patientData(1)

    This page titled 9.6: Reading Mixed Data with textscan was last modified on Tue, 30 Jun 2026 02:12:02 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.