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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

Ken Atwell
Ken Atwell el 30 de Abr. de 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.

Más respuestas (0)

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by