The Experimentor

Experimenting ON Earth

DISPLAYING HTML OUTPUT USING MATLAB

Introduction

Matlab is a great tool for technical computing. For each computation we need to display the output and in most of the case the output is displayed in the console. But what if more flexibility is needed? What if we need to display data in tables or in more attractractive manner? We can do this using matlab web browser and the web command along with the text: protocol.

 Commands

web

web http://asadujjaman.wordpress.com

web(‘text://<html><body>Hello world!</body> </html>’);

Prerequisite HTML knowledge

HTML is the language of the web. I assume that you already know what html is.

Now I show how to create a table in HTML:

<html>

<body>

<table>

<tr>

<th>Header1</th> <th>Header2</th>

</tr>

<tr>

<tr>Data1</tr><tr>Data2</tr>

</tr>

</table>

</body>

</html>

The <tr> tag specifies a row, <th>  specifies a header cell and <td> specifies an ordinary cell.

The Idea

We can exploit the web(‘text://<html>…</html> ’) command and our html knowledge to display html output in the matlab browser. We produce an HTML string as our computation proceeds and then pass the string to the browser.

A Numerical Example

Last week we solved the problem of computing the roots of a function using a numerical method called SIMPLE FIXED-POINT ITERATION in our computer lab (RUET). We needed to show results of each iteration. We just used disp(sprintf(‘…’ )). But I searched for something more pleasurable. And there is no doubt that I found some satisfactory idea.

The Code

function [x,html]=SimpleFPI(gx,es,max)

html=’<html><head><title>Simple Fixed Point Iteration</title>’;

html=strcat(html,’</head><body><table border=”1″>’);

html=strcat(html,’<tr><th><i>i</i></th><th><i>x<sub>i</sub></i></th><th><i>&epsilon;<sub>a</sub>(%)</i></th></tr>’);

x=0;

i=0;

ea=100;

while((ea>es)&&(i<max))

html=sprintf(‘%s<tr><td>%f</td><td>%f</td><td>%f</td></tr>’,html,i,x,ea);

xold=x;

x=eval(gx);

ea=100*abs(xold-x)/x;

i=i+1;

end

html=sprintf(‘%s</table><br>The root is %f</body></html>’,html,x);

if(ea>es)

disp(sprintf(‘Method did not converge’));

x=NaN;

end

end

Using the function

Suppose we need to find the roots of f(x) = 0 where, f(x) = e-x-x.

We arrange this in x=g(x) format and g(x) becomes e-x or exp(-x).

The SimpleFPI function takes three arguments:

The function g(x), maximum approximate error (ea) and maximum number of iterations in case the function doesn’t converge.

It outputs two results:

The root and an HTML string describing the process.

From MATLAB

>>> [root, html]=SimpleFPI(‘exp(-x)’,.001,100);

>>>web(sprintf(‘text://%s’,html));

The Output

i xi εa(%)
0.000000 0.000000 100.000000
1.000000 1.000000 100.000000
2.000000 0.367879 171.828183
3.000000 0.692201 46.853639
4.000000 0.500474 38.309147
5.000000 0.606244 17.446790
6.000000 0.545396 11.156623
7.000000 0.579612 5.903351
8.000000 0.560115 3.480867
9.000000 0.571143 1.930804
10.000000 0.564879 1.108868
11.000000 0.568429 0.624419
12.000000 0.566415 0.355568
13.000000 0.567557 0.201197
14.000000 0.566909 0.114256
15.000000 0.567276 0.064752
16.000000 0.567068 0.036739
17.000000 0.567186 0.020831
18.000000 0.567119 0.011816
19.000000 0.567157 0.006701
20.000000 0.567135 0.003800
21.000000 0.567148 0.002155
22.000000 0.567141 0.001222

The root is 0.567145
Conclusion

This is the very basic approach. More advanced technique like CSS can be used for better flexibility.

No comments yet.

Leave a comment