6. 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.
Loading Data Sets
Sometimes our work as engineers requires us to analyze a large data set, or even multiple data sets. Matlab is a powerful tool for performing such analyses. Moreoever, if a Matlab script is well designed then it can be used and re-used to analyze different data sets. That is, a script can be used many, many times.
For example, in this course, you will download large data sets from the National Oceanic and Atmospheric Administration’s (NOAA) National Data Buoy Center (https://www.ndbc.noaa.gov/). These files include measurements of wind speed, air temperature, water temperature, wave weight, atmospheric pressure, and much more. You will select a unique buoy site, for a given calendar year, to perform analysis of its environmental conditions. A well designed script would be able to load in any year of data from that site, or even another location, and perform the analysis by running the code — without any editing of the code, aside from changing which file is loaded into Matlab.
In order for Matlab to load a data set, it needs to know where that file is located on your computer. This is easily accomplished by maintaining good file management practices. It is highly recommended that you make a file folder on your computer for this course, with clearly named subfolders for each homework assignment (e.g. ENGR448_HW3). The easiest way for Matlab to find the file you want to load is to save it in the same location (folder) as your m-file. (Alternatively, you can add a pathname to the load command if you prefer to save the data file elsewhere.)
To load numerical data from an Excel file, you can use the following line of code:
>> numericalVars = readmatrix('NameOfExcelFile', 'Sheet', ’tabname’);
Note that the Excel file name and tab name should contain no spaces and must be placed within single quotes. Also, the variable assignment name (in this example it is called
numericalVars
) should contain no spaces and cannot start with a number.
It may be the case that you would like to analyze a portion of a large data set. In that case, there is no reason to load the entire file, especially if you know what portion you are interested in, and also to save on computer memory. To load specific portions from an Excel file, you can use the following line of code:
>> numericalVars = readmatrix('NameOfExcelFile', 'Range', ’A3:G92’);
In the above example, you hand Matlab the range of cells in Excel, in this case from A3 through G92.
>> numericalVars = readmatrix('NameOfExcelFile', 'Range', [3 1 45 98]);
In the case above, you are handing Matlab four values inside of hard brackets, which are the [firstRow firstColumn lastRow lastColumn].
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)