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

3 ビュー (過去 30 日間)
nitin arora
nitin arora 2015 年 4 月 29 日
コメント済み: nitin arora 2015 年 4 月 30 日
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.

採用された回答

Ken Atwell
Ken Atwell 2015 年 4 月 30 日
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.
  1 件のコメント
nitin arora
nitin arora 2015 年 4 月 30 日
Thanks a lot Ken. It works. You rock. You saved me a lot of time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by