reading in text files

2 次查看(过去 30 天)
jfrazie9
jfrazie9 2018-3-26
评论: jfrazie9 2018-3-27
I am try to read in 250 *.txt files. Each file resembles the attached picture. I have tried the following;
for k = 1:250
textFilename = sprintf('C58501_line_ssF_%04d.txt',k);
M = dlmread(textFilename,' ',1,0);
end
This reads the files in but not in a usable format. How do I go about loading these files in as 250 seperate files each one being a matlab file of the *.txt file without the header?
Thank you in advance.
  14 个评论
Walter Roberson
Walter Roberson 2018-3-27
Please attach a sample file.
jfrazie9
jfrazie9 2018-3-27
Here is a sample file. This file contains the same columns as the other 249, the sixth column will change slightly as it is the time of the particle flow path in days. I am concerned about columns 1,2,3 and 6 where column 1 is the ID, column 2 is the X position in m and column 3 is the Y position in meters. I need to import all the files as they are, reduce to those 4 columns, correct them so they are in a UTM layout by adding 10000 to every value in column 2 and 3 and sort out the rows where the ID is a set of numbers like 2,4,8,14 and 42 for example.

请先登录,再进行评论。

回答(1 个)

Jeremy Hughes
Jeremy Hughes 2018-3-27
You should try tabularTextDatastore assuming everything has the same format.
ds = tabularTextDatastore(pathToFiles)
ds.SelectedVariableNames = ds.SelectedVariableNames([1,2,3,6]);
while hasdata(ds)
T = read(ds)
% do stuff
end
  8 个评论
Walter Roberson
Walter Roberson 2018-3-27
dinfo = 'C58501_line_ssF_%04d.txt';
pathToFiles = {dinfo.name};
ds = tabularTextDatastore(pathToFiles)
ds.SelectedVariableNames = ds.SelectedVariableNames([1,2,3,6]);
while hasdata(ds)
  T = read(ds)
  % do stuff
end
jfrazie9
jfrazie9 2018-3-27

This returns an error of 'Struct contents reference from a non-struct array object'. Coming from the line

pathToFiles = {dinfo.name};

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by