How to selectively import a column from a text file containing lot of headers

26 views (last 30 days)
nitin arora
nitin arora on 29 Apr 2015
Commented: nitin arora on 30 Apr 2015
I have a text file containing 57 header values and I want to selectively import 2 columns only. These are "X-Centroid(µm)"(column 43) and Y-Centroid(µm) (column 44). The number of rows is variable depending on the number of features detected in an image. Currently, I am copy-pasting my data from text file into excel and using xlsread to import data into matlab. This is a time consuming step that I went to get rid of. I want to import data from text file directly as I need to automate this step for a no of text files. I am using textscan but I am fairly new to matlab and having hard time figuring it out. Please advice.

Accepted Answer

Ken Atwell
Ken Atwell on 30 Apr 2015
You can import all 57 columns with the following:
txt=fileread('1.TXT');
% Read 57 numeric columns of data
textscan(txt, repmat('%f', [1 57]), 'Delimiter', '\t', 'HeaderLines', 1);
If you only want to import those two rows, things get a tad more complicated:
txt=fileread('1.TXT');
% Skip 42 columns of numeric data, read two columns, then ignore the rest
format=[repmat('%*f', [1 42]) '%f%f%*[^\n]'];
textscan(txt, format, 'Delimiter', '\t', 'HeaderLines', 1)
I hope this helps.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!