Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

How do I read Text from HTML file ?

Asked by vish

vish (view profile)

on 7 Feb 2011

I would like to know how is it possible to read only text from HTML file. I assume matlab reads the source code of the file due to which along with the actual text, it also reads the tags and other script related data. Please let me know how can this be solved.



vish (view profile)



2 Answers

Answer by Ned Gulley

Ned Gulley (view profile)

on 7 Feb 2011
Accepted answer

Try something like this:

url = '';
html = urlread(url);
% Use regular expressions to remove undesired markup.
txt = regexprep(html,'<script.*?/script>','');
txt = regexprep(txt,'<style.*?/style>','');
txt = regexprep(txt,'<.*?>','')

Once you have all the HTML, you can sift out the style and script blocks. Then remove any remaining tags and the result is reasonably clean. It won't be perfect, but it's easy to tune from here.


Michael Lenihan

Michael Lenihan (view profile)

on 1 Dec 2011

Hi Ned, thats some useful code. Im trying to get a number from HTML code rather than txt. Any ideas?

Walter Roberson

Walter Roberson (view profile)

on 1 Dec 2011

Same algorithm. You would use str2double() or sscanf() on the txt to convert to numeric form.

Ned Gulley

Ned Gulley (view profile)

Answer by Kaustubha Govind

Kaustubha Govind (view profile)

on 7 Feb 2011

if the HTML file consists of valid XML, you might be able to use xmlread.


Kaustubha Govind

Kaustubha Govind (view profile)

Contact us