How to selectively import a column from a text file containing lot of headers
1 次查看(过去 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.
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!