reading in text files

1 回表示 (過去 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 Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by