{"id":193,"date":"2008-02-18T07:52:57","date_gmt":"2008-02-18T12:52:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/02\/18\/interactive-web-pages-in-matlab-part-2\/"},"modified":"2016-04-03T15:34:32","modified_gmt":"2016-04-03T19:34:32","slug":"interactive-web-pages-in-matlab-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/02\/18\/interactive-web-pages-in-matlab-part-2\/","title":{"rendered":"Interactive web pages in MATLAB, part 2"},"content":{"rendered":"<p><a href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/11\/interactive-web-pages-in-matlab-part-1\/\">Last time<\/a>, I described how to use static hyperlinks to run MATLAB commands from the web browser (remember: this only works in MATLAB&#8217;s web browser). This time I will show you how to use JavaScript to assemble and execute <tt>matlab:<\/tt> commands.<\/p>\n<p>How do we move beyond using explicit hyperlinks for issuing MATLAB commands? The hyperlink-click equivalent operation in JavaScript is setting the <tt>document.location<\/tt> property to the desired URL.<\/p>\n<p>The following example uses the web page\u2019s load event to trigger the execution of the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/surf.html\"><tt>surf<\/tt><\/a> command. When the page is loaded in MATLAB&#8217;s web browser, a figure window will be automatically created with the surface plot.<\/p>\n<p>When you open this <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/matlabfromjs1.html\">example file (right-click to save) <\/a> in the MATLAB web browser, which includes the code below&#8230;<\/p>\n<p><code>&lt;html&gt;<br \/>\n&lt;head&gt;&lt;title&gt;Calling MATLAB from JavaScript 1&lt;\/title&gt;&lt;head&gt;<br \/>\n&lt;body onload=\"document.location='matlab:surf(peaks)'\"&gt;&lt;\/body&gt;&lt;\/html&gt;<\/code><\/p>\n<p>You&#8217;ll see a blank browser window (there&#8217;s no real displayable content in the file), and then you&#8217;ll see a figure window popup:<\/p>\n<div align=\"center\">\n<table>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_3.jpg\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_3_small.jpg\" alt=\"matlabfromjs1.html in the MATLAB web browser\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/a><\/td>\n<td><a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_4.jpg\"><br \/>\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_4_small.jpg\" alt=\"surf(peaks) in the figure window\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><br \/>\n<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>One thing to notice in this example is that the JavaScript function in the HTML <a href=\"http:\/\/www.w3.org\/TR\/html401\/interact\/scripts.html#adef-onload\"><tt>onload<\/tt><\/a> event is specified as a String, and the document URI is a separate string. The single quotes (&#8216;) are the way to specify a string within a double-quoted (&#8220;) string, and has nothing to do with MATLAB strings, in this example.<\/p>\n<p>How do we now make the MATLAB command dynamic so the web page user can influence what happens? For starters, we can read in the value of a control and use that in the URI string.<\/p>\n<p>The next <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/matlabfromjs2.html\">example (right-click to save)<\/a> uses HTML forms to present controls to a user. When the button is pressed, MATLAB will plot (in a figure window) a sine wave of the given amplitude and frequency and plot appearance.<\/p>\n<div align=\"center\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_1.jpg\" alt=\"MATLAB Web Browser with sine-wave demo\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/div>\n<p>Pressing the &#8220;Plot the Sine Wave&#8221; button yields the following plot:<\/p>\n<div align=\"center\"><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/michael_katz_interactive_web_pages_part_2\/interactive_web_pages_part_2_2.jpg\" alt=\"matlabfromjs2.html figure window output\" align=\"center\" border=\"0\" hspace=\"5\" vspace=\"5\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>In the header I&#8217;ve created the following script block:<br \/>\n<code><br \/>\n&lt;script type=\"text\/javascript\"&gt;<br \/>\nfunction drawSine() {<br \/>\ncommandURI=\"matlab:t=linspace(0,1,100);\";<br \/>\ncommandURI+=\"plot(t,\";<br \/>\ncommandURI+=document.sine.amplitude.value;<br \/>\ncommandURI+=\"*sin(t*2*pi*\";<br \/>\ncommandURI+=document.sine.frequency.value + \")\";<br \/>\ncommandURI+=\",'\" + document.sine.color.value +<br \/>\ndocument.sine.linestyle.value +<br \/>\ndocument.sine.markerstyle.value + \"'\";<br \/>\ncommandURI+=\");\";<br \/>\ndocument.location=commandURI;<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>A few key points about this code include:<\/p>\n<ol>\n<li>The code that builds the <tt>matlab:<\/tt> URI and executes it as a separate JavaScript function <tt>drawSine()<\/tt> in the web page\u2019s header.<\/li>\n<li>For readability I\u2019ve broken up the command into several lines and used the <tt>+=<\/tt> syntax for String concatenation.<\/li>\n<li>I used the direct naming syntax for accessing the data in the form elements. This syntax requires us to give an explicit <tt>name<\/tt> to each of our form and input elements.<\/li>\n<li>The JavaScript code does not check for valid data in the input boxes, and bad values will produce an error in the Command Window (which may not be obvious to a person working only looking in the web browser).<\/li>\n<\/ol>\n<p>Inside the body&#8217;s form, there are two text inputs, three choice elements, and a button input. The following code creates the button:<\/p>\n<p><code><br \/>\n&lt;input type=\"button\" onclick=\"drawSine()\" value=\"Plot the Sine Wave\"&gt;<br \/>\n<\/code><\/p>\n<p>This is the starting point for dynamically building the MATLAB command URI strings. I&#8217;ve shown you how to use an HTML form to gather input to pass to a MATLAB function. Let us know if you know of other interesting usages of HTML and MATLAB, or run into something tricky.<\/p>\n<p>Next time I\u2019ll complete the tutorial by placing the figure back into in the web page, creating a full GUI.<\/p>\n<p>Check out the first part of this entry:<\/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<\/ul>\n<p>For general information on programming HTML forms, see:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.w3schools.com\/html\/html_forms.asp\">http:\/\/www.w3schools.com\/html\/html_forms.asp<\/a><\/li>\n<\/ul>\n<p>For general information on programming in JavaScript, see:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.w3schools.com\/js\/default.asp\">http:\/\/www.w3schools.com\/js\/default.asp<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Last time, I described how to use static hyperlinks to run MATLAB commands from the web browser (remember: this only works in MATLAB&#8217;s web browser). This time I will show you how to use&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/02\/18\/interactive-web-pages-in-matlab-part-2\/\">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\/193"}],"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=193"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"predecessor-version":[{"id":3409,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/193\/revisions\/3409"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}