Filter löschen
Filter löschen

Trying to import multiple text files with odd formatting

2 Ansichten (letzte 30 Tage)
So I have a years worth of data in the format of the file give and I only need 2 variables but I need to feed that data into Matlab, aside from manually doing so, is there a way to do so? Just reading the .txt file in to Matlab yields an unusable format. Thank you!
  4 Kommentare
Jan
Jan am 8 Jun. 2018
Bearbeitet: Jan am 8 Jun. 2018
@Cameron Power: You do not have to modify the files. As soon as you define clearly and uniquely, what you want to extract, it is possible to solve with some lines of Matlab code. I do not start to post a solution as long, as I have to guess, what you exactly need.
Do this manually for the posted example input.
Cameron Power
Cameron Power am 8 Jun. 2018
Bearbeitet: Cameron Power am 8 Jun. 2018
This is the relevent data that I need from each of the 6000 files.
note: Just the date and time and not the file start/end prefix is needed

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 8 Jun. 2018
This code will work. It's a custom reader I wrote for you. It works at least for the one file you attached, and maybe for others depending on how much, if any, their format varies from the one you attached.
% Type file to command window.
fullFileName = 'test.txt';
type(fullFileName) % OPTIONAL!
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
fprintf('Processing line: "%s"...\n', textLine);
if contains(textLine, 'File start time')
fileStartTimes = sscanf(textLine, 'File start time : %f %f %f %f %f');
elseif contains(textLine, 'File ending time')
fileEndingTimes = sscanf(textLine, 'File ending time : %f %f %f %f %f');
elseif contains(textLine, 'deg')
% Read the next line which has the actual numbers we need on it.
textLine = fgetl(fileID);
numbers = sscanf(textLine, '%f %f');
% Extract out WDIR and WSPD.
WDIR = numbers(1);
WSPD = numbers(2);
% We're all done with this file, so break out (quit reading lines from it).
break;
end
% Read the remaining lines of the file.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
  4 Kommentare
Cameron Power
Cameron Power am 9 Jun. 2018
Whichever way you think is more efficient works, but every file is almost identical to the format attached to this comment.
Image Analyst
Image Analyst am 9 Jun. 2018
That file is vastly different than the one you first gave. They're not even remotely the same! I think I gave you the general idea for how to read a custom format file line-by-line and to extract the numbers so I think you just need to follow my example and adapt it to this totally different format. It's such a complicated format that it would take me a long time to write a custom reader for it, so I think you can now do that just as well as I can, now that you know what to do. Good luck.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 7 Jun. 2018
See the FAQ for two code snippets to read in multiple files: https://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
In the loop, put your file reading code. It can use csvread(), importdata(), readtable(), or whatever.
  1 Kommentar
Cameron Power
Cameron Power am 7 Jun. 2018
I`m sorry but the issue is not the importing of the files, it is the format of the table when I try to import the files. All I want to do is isolate the needed variables across all 5000+ text files.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by