Searching for MATLAB Examples with Google
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 or on Sean and Jiro’s awesome Pick of the Week or maybe the MATLAB Examples page. But after that there’s always Google and the great wild web. Google casts a wide net, so it’s useful to know some techniques for scoping the search down.
Obviously you can just enter the search term “matlab”, but that mostly returns information about the product rather than what we want: code written by other MATLAB users. To find that, it’s good to know about published M-files. Publishing in MATLAB is the process of generating an HTML document that corresponds to your .m script.
There are piles of 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 “Published with MATLAB” at the bottom of the document. So we can search for that term with the help of Google. This pulls in the files we’re looking for as surely as fish on a line.
Search Term: “published with matlab“
I sometimes like to limit my search to files that are not already on the MathWorks site. The advanced Google search directive “inurl” is good for this. If I say “-inurl:mathworks”, then I’ll only get documents that do not have “mathworks” in the URL.
Search Term: “published with matlab” -inurl:mathworks
That’s looking pretty good. Still, it’s a lot of work to sift through these results one by one. I want more immediate gratification! Let’s make it an image search.
Now that’s what I’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 Predator-Prey analysis created by Tobias von Petersdorff at the University of Maryland.
Here’s another good one: Yanghong Huang’s page of ODE examples. Yanghong teaches at the University of Manchester in the UK.
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’s called “grabcode”, and it works like this.
>> url = 'http://personalpages.manchester.ac.uk/staff/yanghong.huang/teaching/MATH36032/html/labdemo7.html '; >> grabcode(url)
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.
Here’s one last trick. The most recent release of MATLAB (R2016a) includes the ability to create something called a Live Script. It’s a special kind of script that, notebook-style, lets you include both inputs and outputs in the same dynamic document.
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.
If I evaluate the resulting document, I’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.
So there you have it. From the world to your doorstep in a few easy steps. Pretty cool, eh?
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.