Import specific type of text file

1 view (last 30 days)
Pepe
Pepe on 14 Jan 2019
Commented: Guillaume on 17 Jan 2019
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 Comments
Guillaume
Guillaume on 14 Jan 2019
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 on 14 Jan 2019
I've attached it. Thanks for the warning. Please take a look now.

Sign in to comment.

Accepted Answer

Jan
Jan on 14 Jan 2019
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};

More Answers (1)

Guillaume
Guillaume on 14 Jan 2019
Edited: Guillaume on 14 Jan 2019
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 Comment
Guillaume
Guillaume on 17 Jan 2019
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!