MATLAB: Trying to import multiple text files with odd formatting

data importtext file

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!

Best Answer

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);