How do I import specific columns from a text file?

조회 수: 17 (최근 30일)
Dan Getz
Dan Getz 2018년 5월 11일
댓글: Ameer Hamza 2018년 5월 14일
I have a rather lengthy script that I am tasked with fixing. The script pulls data from a .txt file, does some calculations, and plots data.
The script currently uses dlmread to open the .txt file, but this no longer works because a new data format (a notes column text instead of numeric data) was added in the middle of the columns of the text file. Now whenever the script gets to the point of the text file, there is a mismatch in data type and I get this error message.
Error using dlmread (line 147)
Mismatch between file and format character vector.
Trouble reading 'Numeric' field from file
Is it possible to tell Matlab to 'skip' over this one notes column while still using dlmread in the text file? Somehow choose columns 1-10 and 12-25 or something along those lines. I tried using readtable, but that got me a large table that I then couldn't convert back into the double arrays that the script originally started with. Trying to read the table and then convert to an excel spreadsheet didn't work either.
  댓글 수: 1
KSSV
KSSV 2018년 5월 12일
There are many other functions to read text files. You may have a look on textscan, load, importdata etc. Coding depends on how your text file is and what you want. To get more help, attach your text file and mention what you want.

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

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 12일
You can use readtable() to read the data from text file in the form of a table and then delete appropriate columns. For example
data = readtable(filename);
data(:,11) = []; will delete column 11
If then you want to convert the table to a matrix so that it match with the output of dlmread() to make it compatible with remaining code then use
data = table2array(data);
  댓글 수: 2
Dan Getz
Dan Getz 2018년 5월 14일
Thank you. That was the most straightforward way to do it.
Ameer Hamza
Ameer Hamza 2018년 5월 14일
You are welcome.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by