Import specific type of text file

1 visualización (últimos 30 días)
Pepe
Pepe el 14 de En. de 2019
Comentada: Guillaume el 17 de En. de 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 comentarios
Guillaume
Guillaume el 14 de En. de 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 el 14 de En. de 2019
I've attached it. Thanks for the warning. Please take a look now.

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 14 de En. de 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};

Más respuestas (1)

Guillaume
Guillaume el 14 de En. de 2019
Editada: Guillaume el 14 de En. de 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 comentario
Guillaume
Guillaume el 17 de En. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by