{"id":318,"date":"2009-06-22T13:54:16","date_gmt":"2009-06-22T13:54:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/06\/22\/interactive-web-pages-in-matlab-part-4\/"},"modified":"2009-06-23T11:53:06","modified_gmt":"2009-06-23T11:53:06","slug":"interactive-web-pages-in-matlab-part-4","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/06\/22\/interactive-web-pages-in-matlab-part-4\/","title":{"rendered":"Interactive web pages in MATLAB, part 4"},"content":{"rendered":"<p>A few weeks ago <a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/25\/interactive-web-pages-in-matlab-part-3\/#comment-6266\">Ashwin<\/a> commented on my <a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/25\/interactive-web-pages-in-matlab-part-3\/\">Interactive web pages in MATLAB<\/a> series from last year asking about computing data in MATLAB and putting back into a web page. I didn&#8217;t take the time back then how to explain it because it&#8217;s not the most straightforward and requires a bit of javascript. <\/p>\n<p>So without further ado, I&#8217;m going to demonstrate one way to take data round-trip from a web page through MATLAB (all running in the same MATLAB session). The main piece of this method is that I am going to have a start function which dynamically creates a web page which loads the desired workspace data. This dynamic page then loads a static page, which is the GUI the user interacts with. This page calls a callback function which evaluates the data from the page and starts the cycle over again. Here is the data flow.<\/p>\n<div align=\"center\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_4\/interactive_pages_flow.png\" alt=\"function flow\"><\/div>\n<p><strong>The main function<\/strong><br \/>\nThe purpose of the main function is to be the entry point to the web GUI. It takes data passed in and puts it into a dynamically created web page, that loads our static page. This process could be simplified by creating the whole html part dynamically, but that looses the benefit of using a better html-editing tool to create a nice looking web page (this is not an example of that).<\/p>\n<p>The tricky part is that we are storing our value to be used in the main GUI in a <a href=\"http:\/\/www.w3schools.com\/tags\/tag_meta.asp\"><tt>meta<\/tt> tag<\/a>. This allows us to look it up from a javascript method in the main page. The main page is loaded in a frame so that it still has access to the meta tag created here. Otherwise we would have to write that meta tag directly to the main page. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> matlabfromjs4(value)\r\n<span style=\"color: #228B22\">%matlabfromjs4 load a web gui to print out the result of the magic command<\/span>\r\n\r\n<span style=\"color: #0000FF\">if<\/span> nargin == 0\r\n    <span style=\"color: #228B22\">%if no arguments (i.e. the first time) then do not load any data<\/span>\r\n    meta = <span style=\"color: #A020F0\">''<\/span>;\r\n<span style=\"color: #0000FF\">else<\/span>\r\n    <span style=\"color: #228B22\">%otherwise put it in a meta tag<\/span>\r\n    meta = value;\r\n<span style=\"color: #0000FF\">end<\/span>\r\nmeta = sprintf(<span style=\"color: #A020F0\">'&lt;meta id=\"valueToPutInPage\" content=\"%s\"&gt;'<\/span>, meta);\r\n\r\n<span style=\"color: #228B22\">%dynamically generate the html and write it out a temporary file<\/span>\r\nfile = tempname;\r\nfid = fopen(file,<span style=\"color: #A020F0\">'w+'<\/span>);\r\nfprintf(fid,[<span style=\"color: #A020F0\">'&lt;html&gt;\\n\\t&lt;head&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t\\t&lt;title&gt;Calling MATLAB from JavaScript 4&lt;\/title&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t%s\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t&lt;\/head&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t\\t&lt;frameset&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t\\t\\t&lt;frame name = \"main\" src=\"%s\"&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\t\\t&lt;\/frameset&gt;\\n'<\/span><span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'&lt;\/html&gt;'<\/span>],meta,[<span style=\"color: #A020F0\">'file:\/\/\/'<\/span> which(<span style=\"color: #A020F0\">'matlabfromjs4.html'<\/span>)]);\r\n<span style=\"color: #228B22\">%matlabjs4.html is the main web gui<\/span>\r\nfclose(fid);\r\n<span style=\"color: #228B22\">%load the page<\/span>\r\nweb([<span style=\"color: #A020F0\">'file:\/\/\/'<\/span> file], <span style=\"color: #A020F0\">'-noaddressbox'<\/span>);<\/pre>\n<p><strong>The GUI page<\/strong><br \/>\nWhen loaded in the web browser, this page sets the meta content and loads the <tt>matlabfromjs4.html<\/tt> into the browser. This page <tt>onload<\/tt> queries the value of that tag and writes it into a special <tt>div<\/tt>. This page also has a form element that when a value is typed in the box, it will ask MATLAB to do a computation based on that value (in this case <tt>magic<\/tt>) and what will refresh the page. <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n<span style=\"color: #0000FF\">&lt;html&gt;<\/span>\r\n    <span style=\"color: #0000FF\">&lt;head&gt;<\/span>\r\n        <span style=\"color: #0000FF\">&lt;script<\/span> <span style=\"color: #D82800\">lang=<\/span><span style=\"color: #A020F0\">\"text\/javascript\"<\/span><span style=\"color: #0000FF\">&gt;<\/span>\r\n            function calculatemagic() {\r\n                var magicValue = document.getElementById(\"magic\").value;\r\n                document.location=\"matlab:js4helper(\"+ magicValue +\");\";\r\n            }\r\n            function loadvalues() {\r\n                var pval = parent.document.getElementById(\"valueToPutInPage\");\r\n                return pval.content;\r\n            }\r\n        <span style=\"color: #0000FF\">&lt;\/script&gt;<\/span>\r\n    <span style=\"color: #0000FF\">&lt;\/head&gt;<\/span>\r\n    <span style=\"color: #0000FF\">&lt;body&gt;<\/span>\r\n        Enter a value for: \r\n        <span style=\"color: #0000FF\">&lt;tt&gt;&lt;b&gt;<\/span>magic<span style=\"color: #0000FF\">&lt;\/b&gt;&lt;\/tt&gt; &lt;input<\/span> <span style=\"color: #D82800\">id=<\/span><span style=\"color: #A020F0\">\"magic\"<\/span> <span style=\"color: #D82800\">onblur=<\/span><span style=\"color: #A020F0\">\"calculatemagic()\"<\/span><span style=\"color: #0000FF\">&gt;<\/span>\r\n        <span style=\"color: #0000FF\">&lt;div<\/span> <span style=\"color: #D82800\">id=<\/span><span style=\"color: #A020F0\">\"answer\"<\/span> <span style=\"color: #D82800\">style=<\/span><span style=\"color: #A020F0\">\"background:lightgray;font-family:Courier;\"<\/span><span style=\"color: #0000FF\">&gt;<\/span>\r\n            <span style=\"color: #0000FF\">&lt;script<\/span> <span style=\"color: #D82800\">lang=<\/span><span style=\"color: #A020F0\">\"text\/javascript\"<\/span><span style=\"color: #0000FF\">&gt;<\/span>document.write(loadvalues());<span style=\"color: #0000FF\">&lt;\/script&gt;&lt;\/div&gt;<\/span>\r\n    <span style=\"color: #0000FF\">&lt;\/body&gt;<\/span>\r\n<span style=\"color: #0000FF\">&lt;\/html&gt;<\/span>\r\n<\/pre>\n<p>The <tt>calculateMagic<\/tt> javascript function uses the previously described <tt>matlab:<\/tt> operator to call our helper function <tt>js4helper<\/tt>.<br \/>\n<strong>js4helper<\/strong><br \/>\nThe helper function calculates the output of the <tt>magic<\/tt> function and formats the output is a displayable in html string, and calls our main function again with the string, regenerating the frame page, starting the whole process over.<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> js4helper(value)\r\n<span style=\"color: #228B22\">%js4helper takes the value from the webpage, find the answer and formats it<\/span>\r\n\r\n<span style=\"color: #0000FF\">if<\/span> ~exist(<span style=\"color: #A020F0\">'value'<\/span>,<span style=\"color: #A020F0\">'var'<\/span>)\r\n    <span style=\"color: #228B22\">%need to check that no valid input was given in case input box was blank.<\/span>\r\n    magicVal = <span style=\"color: #A020F0\">''<\/span>;\r\n<span style=\"color: #0000FF\">else<\/span>\r\n    val = magic(value);\r\n    [r,c] = size(val);\r\n    format = [repmat(<span style=\"color: #A020F0\">'%i '<\/span>,1,r) <span style=\"color: #A020F0\">'&lt;br&gt;'<\/span>];\r\n    magicVal = sprintf(format, val);\r\n<span style=\"color: #0000FF\">end<\/span>\r\nmatlabfromjs4(magicVal);<\/pre>\n<p>That&#8217;s the basics of creating dynamic html GUIs in MATLAB. For extra credit I&#8217;m leaving you with two assignments:<br \/>\n1. Use base64 encoding to add an image to the web page without having to create an image file.<br \/>\n2. Use ActiveX on Windows to hook a web page running in IE up to a MATLAB session.<\/p>\n<p><strong>UPDATE:<\/strong> (6\/23\/09) Download the files here: <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_4\/matlabfromjs4.zip\">matlabfromjs4.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago Ashwin commented on my Interactive web pages in MATLAB series from last year asking about computing data in MATLAB and putting back into a web page. I didn&#8217;t take the time back&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/06\/22\/interactive-web-pages-in-matlab-part-4\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[40,245],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/318"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}