Import data from text sheet to a variable

조회 수: 5 (최근 30일)
santosh v
santosh v 2015년 7월 25일
댓글: santosh v 2015년 7월 26일
I have an text file with following entries
id pid sigma temp value dist
12 369 1740 528 187 9.01
19 741 730 519 206 9.1
13 405 1003 467 158 10.10
32 2179 419 445 191 11.1
18 721 397 657 120 13.2
22 1478 365 600 304 13.3
41 2520 1180 526 179 13.41
i only want to import the dist value to a variable
i.e i want to have my variable
x = [9.01 9.1 10.10 11.1 13.2 13.3 13.41]
Is there a way I could only the 6th column entries from text file to matlab variable x.

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 25일
편집: Azzi Abdelmalek 2015년 7월 25일
fid = fopen('file.txt')
data = textscan(fid, '%*s %*s %*s %*s %*s %f %*[^\n]','HeaderLines',1);
fid = fclose(fid);
  댓글 수: 5
Walter Roberson
Walter Roberson 2015년 7월 26일
Navya, unfortunately dlmread() cannot be used for files which contain any text at all, even if the row and columns requested are after where the text is.
dlmread() uses an undocumented feature of textscan() that only works when the columns are purely numeric.
In the cases that dlmread() does work for, it is a matter of the code reading and converting the values first, and then throwing away the parts that are not wanted. As I describe above, it is not possible to read only the desired column, that everything must be read for text files, but the other parts can be discarded.
santosh v
santosh v 2015년 7월 26일
@navya: thanks for the link.. it worked for my case!

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by