필터 지우기
필터 지우기

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

조회 수: 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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by