Import specific type of text file

1 次查看(过去 30 天)
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