Parsing Through HTLM Style .txt File
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm trying to read in a txt file with a series of messages describing what is happening during a sporting event. The general format seems to be based on HTML but the file is given as .txt. I'm able to open the file and save it as a variable using fscanf (not sure if this is the most appropiate way for this application). I'm having trouble wrapping my head around how to search for blocks of code that I want to record for further analysis. For example one of things ill be doing is looking for a block like the one below.
<FLAG>
<ID>e9f40b4b-decd-4b47-ae83-e6f9c972c570</ID>
<DT>01.07.2023 16:30:00.000</DT>
<FL>Green</FL>
<LV>Track</LV>
<FI />
</FLAG>
So in plain English i want to look for every time there is <FLAG> and then record the time of day which is denoted by <DT>. I'm pretty rusty with my MATLAB never any reading from .txt files when i was at my best so I think once I get the hand of reading and searching through the file I'll be able to figure it all out from there.
댓글 수: 0
답변 (2개)
Ganesh
2024년 6월 13일
편집: Ganesh
2024년 6월 13일
You can consider using the "readstruct()" function in MATLAB. It allows you to input an "XML" file and parses it into a structure. Please find the code implementation of the same below:
a = readstruct('Data.txt',FileType="xml")
% You can now access the fields like any normal MATLAB structure
a.FLAG(1)
for i=1:length(a.FLAG)
disp(num2str(i)+ " -- "+a.FLAG(i).ID)
end
You can read up more on readstruct from the following documentation:
댓글 수: 3
Ganesh
2024년 6월 14일
Do refer to the ".txt" file attached. You might be missing to enclose the whole data within a tag.
Hassaan
2024년 6월 13일
% Open the file
fileID = fopen('your_file.txt', 'r');
if fileID == -1
error('Could not open file');
end
% Read the file
fileContent = fscanf(fileID, '%c');
fclose(fileID);
% Find all occurrences of <FLAG> blocks
flagStart = strfind(fileContent, '<FLAG>');
flagEnd = strfind(fileContent, '</FLAG>');
for i = 1:numel(flagStart)
% Extract the FLAG block
flagBlock = fileContent(flagStart(i):flagEnd(i)+6); % +6 to include </FLAG>
% Find and extract the time of day (DT)
dtStart = strfind(flagBlock, '<DT>') + 4;
dtEnd = strfind(flagBlock, '</DT>') - 1;
timeOfDay = flagBlock(dtStart:dtEnd);
% Display or save the time of day
disp(['Time of day: ' timeOfDay]);
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
댓글 수: 2
Hassaan
2024년 6월 13일
@Adam Holland You are welcome.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
참고 항목
카테고리
Help Center 및 File Exchange에서 Adding custom doc에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!