{"id":194,"date":"2008-02-25T07:45:25","date_gmt":"2008-02-25T12:45:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/02\/25\/interactive-web-pages-in-matlab-part-3\/"},"modified":"2016-10-26T11:03:20","modified_gmt":"2016-10-26T15:03:20","slug":"interactive-web-pages-in-matlab-part-3","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/02\/25\/interactive-web-pages-in-matlab-part-3\/","title":{"rendered":"Interactive web pages in MATLAB, part 3"},"content":{"rendered":"<p>As promised in my <a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/18\/interactive-web-pages-in-matlab-part-2\/\">last post<\/a>, this week we will complete our GUI by placing a dynamically-generated MATLAB figure back into the web page.<\/p>\n<p>To show this, we will build upon the last example from last week. In that example, the page generates a sine wave with its settings derived from the HTML form. Instead of displaying the plot in a separate figure window, we&#8217;ll create it and print it to a file, where it will then be loaded back into the web page.<\/p>\n<p>This <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_3\/matlabfromjs3.html\">demo (right-click to save)<\/a> will only work in MATLAB web browser on Windows (sorry, Ken).<\/p>\n<div align=\"center\"><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_3\/interactive_web_pages_part_3_1.png\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_3\/interactive_web_pages_part_3_1_small.png\" alt=\"MATLAB Web Browser with sine-wave demo\" align=\"middle\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><br \/>\n<\/a><\/div>\n<p>Clicking on the &#8220;Plot the Sine Wave&#8221; button causes the following to happen:<\/p>\n<ol>\n<li>The sine and plot are set-up using the form&#8217;s options, as before.<\/li>\n<li>The plot is exported to a PNG-file on disk, using the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/print.html\"><tt>print<\/tt><\/a> command.<\/li>\n<li>The latest version of the image on disk is shown in the page, by refreshing an HTML element.<\/li>\n<\/ol>\n<p>In order for these actions to work smoothly, the code makes use of a few tricks:<\/p>\n<ul>\n<li>In the JavaScript function that creates the plot, we added some figure manipulations to the issued MATLAB command. The following code:<br \/>\n<code><br \/>\ncommandURI+=\"f=figure;set(f,'Visible','off','units','pixels',\" +<br \/>\n\"'Position',[0 0 270 210],'PaperPositionMode','auto');\";<br \/>\n<\/code><\/p>\n<ol>\n<li>Creates a new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/figure.html\"><tt>figure<\/tt><\/a> and saves its handle.<\/li>\n<li>Sets the figure&#8217;s properties so that figure is invisible (and therefore not shown to the user).<\/li>\n<\/ol>\n<\/li>\n<li>This code is followed with the <tt>print<\/tt> command to export the figure to disk. The specified options set the file-type to PNG and the print resolution to be that of the screen.<code><br \/>\ncommandURI+=\"print('sinewave.png','-dpng',sprintf('-f%d',f),'-r0');\";<br \/>\n<\/code><\/li>\n<li>In the page, I&#8217;ve placed an HTML <a href=\"http:\/\/www.w3.org\/TR\/html401\/present\/frames.html#h-16.5\"><tt>iframe<\/tt><\/a> to display the image. The <tt>iframe<\/tt> element allows me to refresh its content without having to reload the entire page, providing a smooth transition when the button is pressed.<code><br \/>\n&lt;iframe name=\"image\" id=\"image\" frameborder=0 marginwidth=0 marginheight=0 scrolling=no width=270 height=210 src=\"\"&gt;<br \/>\n<\/code><br \/>\nThe important properties of this <tt>iframe<\/tt> are: the <tt>id<\/tt>, which lets us get to it later, and the empty <tt>src<\/tt>, which gives us a blank page on startup.<\/li>\n<li>The final piece to this code is the last line of JavaScript in the <tt>drawSine()<\/tt> function. The purpose of the code below is to give MATLAB enough time to process the plot command and make sure the file is saved to disk before it is reloaded. Here we set a timer for 0.5 seconds, and at the timeout replace the image-file source in the <tt>iframe<\/tt>.<code><br \/>\nsetTimeout(\"document.getElementById('image').src='sinewave.png';\",500);<br \/>\n<\/code><\/li>\n<\/ul>\n<p>That&#8217;s pretty much it. I hope you&#8217;ve enjoyed this 3-part series, and good luck creating HTML GUIs in MATLAB. Let us know what kinds of interesting and crazy web pages you can think of.<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/11\/interactive-web-pages-in-matlab-part-1\/\">Interactive web pages in MATLAB, part 1<\/a><\/li>\n<li><a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/18\/interactive-web-pages-in-matlab-part-2\/\">Interactive web pages in MATLAB, part 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>As promised in my last post, this week we will complete our GUI by placing a dynamically-generated MATLAB figure back into the web page.<br \/>\nTo show this, we will build upon the last example from last&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/25\/interactive-web-pages-in-matlab-part-3\/\">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":[18],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/194"}],"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=194"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":4182,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/194\/revisions\/4182"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}