Home

How to Import Selected Rows or Columns into MATLAB

|
|  Updated:  
2016-03-26 08:21:55
|   From The Book:  
MATLAB For Dummies
Explore Book
Buy On Amazon

Sometimes you don’t need an entire file imported into MATLAB, only certain rows and columns of it. The csvread() function provides a straightforward example of how to perform this task.

To see just a range of data displayed, type CSVOutput = csvread(‘NumericData.csv’, 0, 0, [0, 0, 1, 1]) and press Enter. You see the following output:

CSVOutput =
 15 25
 18 29

The first argument to csvread() is the name of the file to read. The second and third arguments are the row and column to start reading. In this case, the example starts with row 0 and column 0. The fourth argument is a matrix that specifies the range of values to read.

The first two values in the matrix must match the second and third argument because they specify the starting point of the range. The second two values provide the ending point of the range.

To give you a better idea of precisely how the range feature works, type CSVOutput = csvread(‘NumericData.csv’, 0, 1, [0, 1, 2, 2]) and press Enter. This time the output changes to show the second and third columns of the data in NumericData.csv:

CSVOutput =
 25 30
 29 33
 35 41

The row and column values that you use with csvread() are zero based. This means that the first row is actually row 0 and the first column is actually column 0. A three-row table would have rows 0 through 2, not 1 through 3. Likewise, a three-column table would contain columns 0 through 2, not 1 through 3.

About This Article

This article is from the book: 

About the book author:

Jim Sizemore is Professor of Physics and Engineering at Tyler Junior College. For over 25 years he s worked in the semiconductor and software industries as a process engineer, device physicist, and software developer and has been teaching college physics, engineering, and math for the last 13 years.

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.