Chapter 9: Inputting and Outputting Data

Up to this point MATLAB has been used to analyze singular user inputs and has only addressed scripts that output a finite number of outputs. To get the most of MATLAB and be able to scale its operation, the user must have the ability to input large data sets and output equally large data sets automatically. This chapter will address how to create codes using excel or comma-separated values (.csv) files for data input and/or output.

Introducing Comma Separated Values Read and Write Functions.

MATLAB has made it easy for the user to place generated sets of data into a separate file or word processing software which can be stored and processed outside of the MATLAB software. A comma-separated values file is a plain text file that contains a list of values that are easily input into text editors such as Microsoft Excel or Apple Sheets. Data that is stored as a variable in MATLAB can easily be exported to a .csv file using the csvwrite command. The notation for the command is included below.

>> csvwrite (‘filename.ext’, X)

The csvwrite command only requires a couple of pieces of information to generate a .csv file. The portion in green is the desired file name for the .csv file. Note that the name ends with the extension .csv, which is required to generate the correct file format.  The portion of the command in blue is the name of the variable storing the data, which is presumably either a matrix or a vector. It is the information contained within variable X that will be output into the file.

While the csvwrite function is easy to use and is effective at generating new .csv files, the command is not helpful if the user would like to input data directly into an existing .csv file. To input data into an existing file, use the writematrix command in place of the csvwrite command. The formatting for the writematrix command is below. Some references will recommend using the dlmwrite function in place of writematrix, though MATLAB is phasing out dlmwrite and it is currently recommended to use the writematrix function.

>> writematrix(M,’filename.ext’)

Within examples of file names in this chapter, notice how each command lists the extension as “.ext”, which is shorthand for “extension” and serves as a general placeholder for any extension. Within examples above, such as csvwrite, it is assumed that the user is outputting data into a comma separated value file. However, the writematrix command can accommodate many varieties of file formats. The following chart lists possible extensions which could be used to specify the file format desired by the user.

Per MathWorks documentation, the following file formats can be used to incorporate text or spreadsheet files.

There are a couple methods that can be used to input data from a .csv file into a MATLAB code. The function csvread operates in a similar manner to the csvwrite function, while the csvread function reads data from a preexisting .csv file and inputs the data into a MATLAB variable. The following shows the options the user has for inputting .csv data into MATLAB.

Option 1:

>> csvread (‘filename.ext’)

Option 2:

>> N = ‘filename.ext’

>> M = csvread (N)

The first option inputs the data from the csv file directly into the MATLAB program. The second option ensures that the data input from the .csv will be stored under variable M. This can easily be modified to read only parts of a .csv file by adding the row and column on the file where MATLAB should input data. This is depicted below where MATLAB will input data starting at the third row and second column of the file.

>> N = ‘filename.ext’

>> M = csvread (N,3,2)


Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

A Guide to MATLAB for ME 160 Copyright © 2022 by Austin Bray and Reza Montazami is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.