필터 지우기
필터 지우기

Trying to import multiple text files with odd formatting

조회 수: 2 (최근 30일)
Cameron Power
Cameron Power 2018년 6월 6일
댓글: Image Analyst 2018년 6월 9일
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
Jan
Jan 2018년 6월 8일
편집: Jan 2018년 6월 8일
@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 2018년 6월 8일
편집: Cameron Power 2018년 6월 8일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 6월 8일
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
Cameron Power
Cameron Power 2018년 6월 9일
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 2018년 6월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 6월 7일
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
Cameron Power
Cameron Power 2018년 6월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by