MATLAB Answers

0

reading in text files

Asked by jfrazie9 on 26 Mar 2018
Latest activity Commented on by 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

Bob Nbob
on 27 Mar 2018

Yes, it is possible to sort things based on specific values of rows. Going back to our previous example:

T{k} = T{k}(T{k}(:,4)==4||T{k}(:,4)==9||T{k}(:,4)==12||T{k}(:,4)==15||T{k}(:,4)==17,:);
% Note that this removes all other values of T{k}. If you want to keep them
% you need to save them as a different variable. Also note that all of the
% right side of the equation is indexing, there is no value adjustment.

Basically, you can include logic challenges in your indexing.

Walter Roberson
on 27 Mar 2018
Please attach a sample file.
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.

1 Answer

Answer by 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

jfrazie9 on 27 Mar 2018
And then normalize columns 2 and 3 of the new output by the addition factors necessary to look at the data?
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 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.