{"id":3821,"date":"2016-06-30T11:44:41","date_gmt":"2016-06-30T15:44:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/community\/?p=3821"},"modified":"2019-04-03T14:19:42","modified_gmt":"2019-04-03T18:19:42","slug":"searching-for-matlab-examples-with-google","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2016\/06\/30\/searching-for-matlab-examples-with-google\/","title":{"rendered":"Searching for MATLAB Examples with Google"},"content":{"rendered":"<p>I want to show you a trick for finding entertaining MATLAB code. Of course I will always recommend that you start looking on the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\">File Exchange<\/a>\u00a0or on <a href=\"https:\/\/blogs.mathworks.com\/pick\/\">Sean and Jiro&#8217;s awesome Pick of the Week<\/a> or maybe the <a href=\"https:\/\/www.mathworks.com\/help\/examples.html\">MATLAB Examples<\/a> page. But after that there&#8217;s always Google and the great wild web. Google casts a wide net, so it&#8217;s useful to know some techniques for scoping the search down.<\/p>\n<p>Obviously you can just enter the search term &#8220;matlab&#8221;, but that mostly returns information about the product\u00a0rather than what we want:\u00a0code written by other MATLAB users. To find that, it&#8217;s good to know about published M-files. <em>Publishing<\/em> in MATLAB is the process of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/publishing-matlab-code.html\">generating an HTML document that corresponds to your .m script<\/a>.<\/p>\n<p>There are piles\u00a0of beautiful published M-files on the web. But how to find them? For this, we will use a special search term. Every published file automatically includes the phrase &#8220;Published with MATLAB&#8221; at the bottom of the document. So we can search for that term with the help of Google. This pulls in the files we&#8217;re looking for as surely as fish on a line.<\/p>\n<p>Search Term:\u00a0<a href=\"https:\/\/www.google.com\/search?q=&quot;published+with+matlab&quot;\">&#8220;published with matlab<\/a>&#8220;<\/p>\n<p>I sometimes like to limit my search to files that are not already on the MathWorks site. The advanced Google search directive &#8220;inurl&#8221; is good for this. If I say &#8220;-inurl:mathworks&#8221;, then I&#8217;ll only get documents that do not have &#8220;mathworks&#8221; in the URL.<\/p>\n<p>Search Term:\u00a0<a href=\"https:\/\/www.google.com\/search?q=&quot;published+with+matlab&quot;+-inurl:mathworks\">&#8220;published with matlab&#8221; -inurl:mathworks<\/a><\/p>\n<p>That&#8217;s looking pretty good. Still, it&#8217;s a lot of work to sift through these results one by one. I want more immediate gratification! Let&#8217;s make it an <a href=\"https:\/\/www.google.com\/search?q=%22published+with+matlab%22+-inurl:mathworks&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;biw=1129&amp;bih=1057\">image search<\/a>.<\/p>\n<p style=\"padding-left: 30px;\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"539\" class=\"alignnone size-full wp-image-3828\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/search-results.png\" alt=\"search-results\" \/><\/p>\n<p>Now that&#8217;s what I&#8217;m talking about! Every single image on the page corresponds to a lovely MATLAB script. Click through and look at a few of them. For instance, maybe I want to take in the <a href=\"http:\/\/www.math.umd.edu\/~petersd\/460\/html\/predprey.html\">Predator-Prey analysis<\/a> created by Tobias von Petersdorff at the University of Maryland.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"http:\/\/www.math.umd.edu\/~petersd\/460\/html\/predprey.html\"><img decoding=\"async\" loading=\"lazy\" width=\"488\" height=\"524\" class=\"alignnone size-full wp-image-3827\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/pred-prey.png\" alt=\"pred-prey\" \/><\/a><\/p>\n<p>Here&#8217;s another good one: Yanghong Huang&#8217;s <a href=\"http:\/\/personalpages.manchester.ac.uk\/staff\/yanghong.huang\/teaching\/MATH36032\/html\/labdemo7.html\">page of ODE examples<\/a>. Yanghong teaches at the University of Manchester in the UK.<\/p>\n<p style=\"padding-left: 30px;\"><a href=\"http:\/\/personalpages.manchester.ac.uk\/staff\/yanghong.huang\/teaching\/MATH36032\/html\/labdemo7.html\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"322\" class=\"alignnone size-full wp-image-3826\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/oscillator.png\" alt=\"oscillator\" \/><\/a><\/p>\n<p>Now I want to take things one step further still. Even if you already knew that you can publish from MATLAB scripts to HTML, you might not know this next part. There is a MATLAB command that lets you extract the original MATLAB code from the HTML. It&#8217;s called &#8220;grabcode&#8221;, and it works like this.<\/p>\n<pre> &gt;&gt; url = 'http:\/\/personalpages.manchester.ac.uk\/staff\/yanghong.huang\/teaching\/MATH36032\/html\/labdemo7.html ';\r\n &gt;&gt; grabcode(url)<\/pre>\n<p>Now the MATLAB script file has been reconstituted and opened up in an unnamed editor buffer. We can run the code for ourselves in our own MATLAB.<\/p>\n<p style=\"padding-left: 30px;\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"484\" class=\"alignnone size-full wp-image-3824\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/editor.png\" alt=\"editor\" \/><\/p>\n<p>Here&#8217;s one last trick. The most recent release of MATLAB (R2016a) includes the ability to create something called a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/what-is-a-live-script-or-function.html\">Live Script<\/a>. It&#8217;s a special kind of script that, notebook-style, lets you include both inputs and outputs in the same dynamic document.<\/p>\n<p>If I save the M-file I got from Manchester as labdemo7.m, then in the Current File Browser, I can right-click and open it up as a Live Script.<\/p>\n<p style=\"padding-left: 30px;\"><img decoding=\"async\" loading=\"lazy\" width=\"452\" height=\"379\" class=\"alignnone size-full wp-image-3823\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/conversion-to-mlx.png\" alt=\"conversion-to-mlx\" \/><\/p>\n<p>If I evaluate the resulting document, I&#8217;ll see something like this. The new document is nice because, in contrast to the static HTML file, you can do your calculations and coding right in place in the document.<\/p>\n<p style=\"padding-left: 30px;\"><img decoding=\"async\" loading=\"lazy\" width=\"501\" height=\"472\" class=\"alignnone size-full wp-image-3825\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/live-script.png\" alt=\"live-script\" \/><\/p>\n<p>So there you have it. From the world to your doorstep in a few easy steps. Pretty cool, eh?<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/community\/files\/search-results.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>I want to show you a trick for finding entertaining MATLAB code. Of course I will always recommend that you start looking on the File Exchange\u00a0or on Sean and Jiro&#8217;s awesome Pick of the Week or&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2016\/06\/30\/searching-for-matlab-examples-with-google\/\">read more >><\/a><\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/3821"}],"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\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=3821"}],"version-history":[{"count":18,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/3821\/revisions"}],"predecessor-version":[{"id":6438,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/3821\/revisions\/6438"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=3821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=3821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=3821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}