9.3: Simple File I/O with load and save

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.3: Simple File I/O with load and save

    MATLAB has two very convenient functions for simple file input and output: load and save. These are beginner-friendly and useful when the file contains only numbers arranged in a clean rectangular format.

    Reading Numeric Data with load

    The load function reads the contents of a file and stores the result in a matrix. The basic format is:

    variableName = load('filename')

    The file name is usually placed inside single quotes. The variable on the left side is the MATLAB variable that will store the imported data.

    Reading Gym Member Data

    Suppose the following data represents the age, weight, and body mass index (BMI) of several gym members. Each row represents one member.

    45 150 21
    33 120 19
    19 200 22
    55 180 25
    22 145 20

    Save this data in a file named members.dat in your current MATLAB folder. Then read the file using:

    myData = load('members.dat')

    MATLAB stores the data as a 5-by-3 numeric array.

    myData =
    
        45   150    21
        33   120    19
        19   200    22
        55   180    25
        22   145    20

    Now that the data is in MATLAB, we can access columns just like we would with any other matrix.

    Example \(\PageIndex{1}\)

    ages = myData(:,1);
    weights = myData(:,2);
    bmis = myData(:,3);

    avgWeight = mean(weights)

    This is one of the main reasons we use file input. We bring data into MATLAB so that we can calculate, visualize, and analyze it.

    Writing Numeric Data with save

    The save function can write numeric data from MATLAB to a file. When creating a text or data file, we use the -ascii option. The format is:

    save filename matrixName -ascii

    Example \(\PageIndex{2}\)

    myMat = rand(2,3);
    save testFile.dat myMat -ascii
    type testFile.dat

    The command type testFile.dat displays the contents of the file in the Command Window.

    Caution

    If the file already exists, save will overwrite it. That means the old contents of the file will be deleted and replaced with the new data.

    Appending Numeric Data with save

    Sometimes we do not want to replace the old file. Instead, we want to add new information to the end of the file. This is called appending. The format is:

    save filename matrixName -ascii -append

    Example \(\PageIndex{1}\)

    myMat = rand(2,3);
    save testFile.dat myMat -ascii

    yourMat = randi([1 5], 2, 3);
    save testFile.dat yourMat -ascii -append

    type testFile.dat


    The second matrix is added below the first matrix in the same file.

    Summary of load and save

    Task

    Simple MATLAB command

    Best for

    Read numeric data

    myData = load('members.dat')

    A file containing only numbers in a rectangular format

    Write numeric data

    save file.dat A -ascii

    Saving a numeric matrix to a text or data file

    Append numeric data

    save file.dat B -ascii -append

    Adding numeric rows to the end of an existing file

    These commands are very useful, but they are not enough for every situation. Many real files contain names, labels, dates, categories, or a mixture of numbers and text. For those files, we need more flexible tools.

    Important Limitation

    The simple file I/O methods in this section work best when every row in the file has the same number of numeric values. That is because MATLAB stores the result in a matrix, and a matrix cannot have missing spots or rows with different lengths.


    This page titled 9.3: Simple File I/O with load and save 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.