Trying to import multiple text files with odd formatting
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Cameron Power
il 6 Giu 2018
Commentato: Image Analyst
il 9 Giu 2018
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 Commenti
Jan
il 8 Giu 2018
Modificato: Jan
il 8 Giu 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.
Risposta accettata
Image Analyst
il 8 Giu 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 Commenti
Image Analyst
il 9 Giu 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.
Più risposte (1)
Image Analyst
il 7 Giu 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.
Vedere anche
Categorie
Scopri di più su Data Import and Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!