Reading only Certain Columns from .CSV

조회 수: 9 (최근 30일)
Megan Stapley
Megan Stapley 2019년 3월 20일
댓글: Walter Roberson 2019년 4월 3일
I'm trying to read in the 1st and 3rd columns of data from a file.
I know this code shows the 2nd and 3rd:
M = csvread('filename.csv', 2, 2)
How do I skip the 2nd column and just display the 1st and 3rd?

답변 (1개)

Walter Roberson
Walter Roberson 2019년 3월 20일
편집: Walter Roberson 2019년 3월 20일
That cannot be done with csvread() or dlmread().
With textscan() you would use a format specification of '%f %*f %f %*[^\n]' . You would probably use cell2mat() around the result of the textscan() call.
With readtable() the way to proceed would be to use detectImportOptions (new as of R2016b). You might have to give the option 'ReadVariableNames', false . Assign the result to a variable, and set the SelectedVariableNames property of the object to [1 3]. Then use readtable() on the .csv file, passing in that options object.
  댓글 수: 18
Megan Stapley
Megan Stapley 2019년 4월 3일
Hi walter,
This works well but for my second application I would prefer not a table. How do I get a matrix?
The table is giving me an error because I am trying to add a greater than operator to find light sensor values greater than 20.
Thanks!
Walter Roberson
Walter Roberson 2019년 4월 3일
What would it mean to use a greater than operator with those text timestamps ?
If you want to test the sensor values you can extract them from the table:
mask = t{:,4} > 20;
which could, for example, be used in the context:
t(t{:.4}>20, [2 4])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by