Today we welcome guest blogger, MATLAB Product Manager Scott Hirsch.
Hi, I’m Scott Hirsch. You may remember me from such blog posts as “Interactive Volume Visualization Tool” and “Default Docked Figures.” I was Doug Hull’s partner on the original Pick of the Week blog from 2004-2006.
I’m excited to step back into the blogging world to chat with you occasionally about MATLAB. As the product management lead for MATLAB, I spend my days working with the MATLAB development team to make sure that the MATLAB we build is the MATLAB that you all want to use. This blog is a great way for you to let us know how we are doing.
Today’s topic is one of my favorite features of MATLAB – the automatic generation of MATLAB code from the MATLAB desktop and other interactive tools. Have you noticed how the easiest way to learn how to do something in MATLAB is often to start from an example that is close to what you want to do? Well, what if that example does EXACTLY what you want it to do? This is one potential promise of automatically generated MATLAB code. You use an interactive tool to say, fit a curve to some data, and you are handed the equivalent MATLAB code. You can use the code as-is to automate the task that you just completed, but you can also dissect, study, and modify the code to learn how to apply it to other problems. This is particularly helpful for those of us who are newer to MATLAB or newer to programming in general.
Here are just a few examples of where MATLAB and Toolboxes will automatically generate code for you:
- Fitting a curve or a surface.
The Curve Fitting Tool (cftool) and Surface Fitting Tool (sftool) in the Curve Fitting Toolbox will generate a function which generates a similar fit for new data.
- Querying data from a database.
The Visual Query Builder (querybuilder) in the Database Toolbox will generate a script which can be used to re-run the query programmatically.
- Importing data.
The Import Wizard (uiimport) will generate a function to import similarly structured data from a different file.
- Customizing the appearance of a plot.
- Plotting data from the Workspace Browser.
The Plot Selector will generate the single line of MATLAB code used to create the plot.
In most cases, you either generate code by selecting a checkbox on the GUI or selecting an option on the File menu as in this example with the Surface Fitting Tool:
Even though I’ve been using MATLAB for 16 years, I have come to rely pretty heavily on the automatically generated MATLAB code for certain tasks. I love the little one-liners pushed to the command window by the plot selector. I find that these help me seamlessly bounce between clicking and typing, depending on what’s easiest at the time. I love that these commands end up in my history, so that I can recall them later just as if I had typed them myself. I’m also a huge fan of the code generated by the new surface fitting tool in the Curve Fitting Toolbox. It somehow seems to do just what I want, and is easy to read at the same time. I already can’t imagine going back to writing this code from scratch.
I’ll admit that I have a harder time using the code generated from customized plots. At times it can be difficult to recognize my intentions in the generated code. What I’ve found works best for me is to generate code for a small portion of a plot to learn the specific commands for that particular feature. You can do this via the context menu in plot edit mode. Just right-click (command-click if using 1-button mouse) on the object of interest and select “Show Code” from the menu. For example, here’s how I would learn the code for adding a text arrow to a plot:
How about you? Do you use (or avoid?) automatically generated MATLAB code? Please share your comments here.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.