Import data from text sheet to a variable
Show older comments
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
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
Image Analyst
on 25 Jul 2015
And then finally:
% Convert cell to nummerical array then transpose into row vector.
x = cell2mat(data)'
Walter Roberson
on 26 Jul 2015
An amplifying remark: when you use text files, it is necessary to read all of the content of the line, but parts can be thrown away without being returned. It is not possible to read just a column in text files. This is a limitation on text files.
The exception to this would be if the text file lines are all exactly the same size and you are not using \r\n line terminators. In such a case once you have read a column on one line you could fseek() forward by the fixed number of characters to reach the beginning of the column in the next line. Some low level routine might still end up reading those characters in-between but that would be a "quality of implementation" matter. If you are using \r\n line terminators then you can probably get it to work with fseek() but the official rules say that you aren't supposed to do it in the case of "text" files (the newline only case can be treated as a binary file within the rules.)
Navya Snigdha Thumma
on 26 Jul 2015
Edited: Walter Roberson
on 26 Jul 2015
Try this
x=dlmread('your datafile name','',1,5);
if your dataset delimiter is tab use '\t' instead if ''
It basically reads your data like matrix [00 01 02 03 04 05; 10 11 12 33........] and if specified it starts reading the block from given number till the end.
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
on 26 Jul 2015
Categories
Find more on Characters and Strings 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!