Import specific type of text file

조회 수: 1 (최근 30일)
Pepe
Pepe 2019년 1월 14일
댓글: Guillaume 2019년 1월 17일
My text file looks like this:
</td></tr><tr><td>2015-05-31 00:00:00</td><td>1.8136
</td></tr><tr><td>2015-05-31 00:01:00</td><td>1.8137
</td></tr><tr><td>2015-05-31 00:02:00</td><td>1.8136
</td></tr><tr><td>2015-05-31 00:03:00</td><td>1.8138
</td></tr><tr><td>2015-05-31 00:04:00</td><td>1.8136
.
.
.
I want to import it to be in two columns: first one a matlab datenum for that date and time and the second one with this decimal number 1.8136 or so.
How can i do that? tnx
There is an attached file. So you can see for every minute in a day there is an observation.
  댓글 수: 4
Guillaume
Guillaume 2019년 1월 14일
xml is a textual format, so is html. From the snippet you show it's clearly some sort of xml or html in that file. Most likely it's html since I'm not sure xml support tables (which your snippet probably is). Note that html is not designed for data transfer, it's a presentation format, so I would recommend a more reliable way to obtain the data.
In any case, to really clarify what is in that file, please attach the full file.
Pepe
Pepe 2019년 1월 14일
I've attached it. Thanks for the warning. Please take a look now.

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

채택된 답변

Jan
Jan 2019년 1월 14일
Str = fileread(FileName);
% Mask the HTML tags:
indI = strfind(Str, '<');
indF = strfind(Str, '>');
M = zeros(size(Str));
M(indI) = 1;
M(indF) = -1;
M = cumsum(M);
M(indF) = 1;
Str(M == 1) = ' ';
% Read the data:
S = textscan(Str, '%s %s %f');
Date = datenum(strcat(S{1}, {' '}, S{2}));
Number = S{3};

추가 답변 (1개)

Guillaume
Guillaume 2019년 1월 14일
편집: Guillaume 2019년 1월 14일
Your text file is a portion of a html file. As commented, html is not designed for data transfer and you would be better off finding a better way to get your data. Typically, websites provide a proper method to access their source data (such as xml or json files).
The following will parse your file. However, it's not a proper html parser so it's very possibly that it would fail on other files that you would obtain the same way. Because html is a presentation format, it could contain extra stuff (such as text formatting) that would make the parsing fail. Again, html is not a suitable format for data transfer and it would be near impossible to write a robust parser.
filecontent = fileread('code=abas&period=30&endtime=2015-06-30.txt'); %read the whole content of the file
rows = regexp(filecontent, '(?<=<tr>).*?(?=</tr>)', 'match'); %extract table rows. Does not allow for <tr> attributes (regex takes too long otherwise)
columns = regexp(rows, '(?<=<td[^>]*>).*?(?=</td>)', 'match'); %extract columns of each row. Allows for <td> attributes but nothing else
rawtable = vertcat(columns{:}); %will error if any of the table row has more or less columns than other rows (allowed in html)
data = table(datetime(rawtable(2:end, 1)), str2double(rawtable(2:end, 2)), 'VariableNames', {'Time', 'rad'})
  댓글 수: 1
Guillaume
Guillaume 2019년 1월 17일
Note that my solution is a lot more robust than the accepted solution (which by the way, does not work when I test it on the provided file) and produces a more modern output.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by