Import data from text sheet to a variable

1 view (last 30 days)
santosh v
santosh v on 25 Jul 2015
Commented: santosh v on 26 Jul 2015
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.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 25 Jul 2015
Edited: Azzi Abdelmalek on 25 Jul 2015
fid = fopen('file.txt')
data = textscan(fid, '%*s %*s %*s %*s %*s %f %*[^\n]','HeaderLines',1);
fid = fclose(fid);
  5 Comments
Walter Roberson
Walter Roberson on 26 Jul 2015
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 on 26 Jul 2015
@navya: thanks for the link.. it worked for my case!

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!