reading in text files

1 view (last 30 days)
jfrazie9
jfrazie9 on 26 Mar 2018
Commented: jfrazie9 on 27 Mar 2018
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 Comments
Walter Roberson
Walter Roberson on 27 Mar 2018
Please attach a sample file.
jfrazie9
jfrazie9 on 27 Mar 2018
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.

Sign in to comment.

Answers (1)

Jeremy Hughes
Jeremy Hughes on 27 Mar 2018
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 Comments
Walter Roberson
Walter Roberson on 27 Mar 2018
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 on 27 Mar 2018

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

pathToFiles = {dinfo.name};

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!