9.10: Storing File Content in an M-File

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    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.10: Storing File Content in an M-File

    Sometimes, especially in a textbook, lecture, or homework assignment, it is inconvenient to ask students to create a separate spreadsheet or data file before running the MATLAB code. One simple alternative is to store the file content directly in an M-file.

    An M-file is not the same as a spreadsheet file. Instead, it is a MATLAB script or function that creates the data as MATLAB variables. This can be useful when the dataset is small and you want all of the information to travel with the code.

    Method 1: Store the data in a script

    One approach is to create a script that defines the data. For example, the following script creates the same patient data used earlier.

    % patientData_mfile.m
    
    patientData = {'LastName', 'FirstName', 'Age', 'Height_cm', 'Weight_kg';
                   'Grey',     'Chris',     24,   172,         75;
                   'Hall',     'David',     31,   168,         68;
                   'Giovanni', 'Luke',      28,   181,         87;
                   'Cat',      'Donna',     35,   165,         72};

    After this script runs, the variable patientData exists in the MATLAB workspace. A second script can then use that variable to write a spreadsheet file.

    patientData_mfile
    writecell(patientData, 'patients_cell.xlsx');

    This is a convenient teaching method because the data is visible directly in the MATLAB script. Students do not need to download or create a separate file before practicing the file-writing commands.

    Method 2: Store the data as a table in a script

    If the data has meaningful column names, we can store it as a table in the M-file. This is often cleaner than using a cell array when the data will be analyzed by columns.

    % patientTable_mfile.m
    
    LastName = ["Grey"; "Hall"; "Giovanni"; "Cat"];
    FirstName = ["Chris"; "David"; "Luke"; "Donna"];
    Age = [24; 31; 28; 35];
    Height_cm = [172; 168; 181; 165];
    Weight_kg = [75; 68; 87; 72];
    
    T = table(LastName, FirstName, Age, Height_cm, Weight_kg);

    After this script runs, the variable T is a MATLAB table. We can write it to a spreadsheet using writetable.

    patientTable_mfile
    writetable(T, 'patients.xlsx');

    This method also makes the code easier to read because the variable names describe the columns of the data.

    Method 3: Store the data in a function

    A slightly more organized approach is to place the data inside a function. This keeps the workspace cleaner because the function returns only the variable we ask for.

    function T = getPatientTable()
    % getPatientTable returns a small patient data table.
    
    LastName = ["Grey"; "Hall"; "Giovanni"; "Cat"];
    FirstName = ["Chris"; "David"; "Luke"; "Donna"];
    Age = [24; 31; 28; 35];
    Height_cm = [172; 168; 181; 165];
    Weight_kg = [75; 68; 87; 72];
    
    T = table(LastName, FirstName, Age, Height_cm, Weight_kg);
    end

    Then another script can call the function and write the table to a spreadsheet.

    T = getPatientTable();
    writetable(T, 'patients.xlsx');

    For small examples, storing data in an M-file can make a lesson easier to manage. For larger datasets, real external files are usually better because they keep data separate from code and are easier to update.

    Note

    Use an M-file to store file content when the data is small, stable, and part of a teaching example. Use a separate data file or spreadsheet when the data is large, collected from an experiment, or likely to change.


    This page titled 9.10: Storing File Content in an M-File was last modified on Tue, 30 Jun 2026 02:12:03 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.