Stuart’s MATLAB Videos

Watch and Learn

Top five productivity practices in MATLAB 6

Posted by Doug Hull,

I teach the “MATLAB 101” course for all of our new technical support engineers at MathWorks. When I come to their computers, I become aware of little tweaks I have made to my working style and MATLAB environment that save me steps, or a few seconds HUNDREDS of times a day. Here are five tips that you can implement right now to make things better for you.
  1. Use shortcuts on desktop
  2. You can add custom shortcuts to the MATLAB desktop to get single-click access to any MATLAB code you run often. I see so many people that leave their MATLAB desktop with just the default shortcuts: noshortcuts.jpg

    I made the following shortcuts by just dragging code from the command window or editor directly to the shortcut bar. You can learn more about making shortcuts here. Here are the shortcuts I find myself using all of the time:

    Gets MATLAB back into a fresh state

    clear
    clc
    close all
    
    Opens the current directory in Windows
    winopen(pwd)
    
    
    Changes directory to a common destination
    cd('C:\Users\dhull\Desktop')
    
    Make all future figure windows start docked
    set(0,'DefaultFigureWindowStyle','docked');
    Make all future figure windows start normally
    set(0,'DefaultFigureWindowStyle','undocked');
  3. Dock windows in the desktop
  4. I see so many people spend a few seconds hunting for the editor, figure windows, etc *every* time they switch contexts. This is so easily avoided, just put MATLAB full screen, dock the desktop components if they aren’t already docked (using the little arrow in the upper right corner of each component)  and rearrange the desktop components until you can see everything at once.
  5. Make throw-away scripts
  6. MATLAB is great because you can use it interactively. However, I see people too often do everything at the command line and forget how they got to whatever state they end up in. They then need to dig around the command history looking for the specific commands they used that worked. If you work in a script, and just keep modifying it as you go, you know exactly how you got to the state you finish in. I also recommend starting the script with
    clear
    clc
    close all
    
    so that you always start in a known state.  One small gotcha – if you are using the debugger, don’t include the clear command at the beginning of your script.  It will clear your debugger breakpoints in addition to clearing your workspace variables.
  7. Use F5 to save and run
  8. The script I mentioned above is a lot easier to iterate on if you just hit F5 to save and run.
  9. Use run configurations
  10. You can click the down arrow on the right side of this button

    runconfig.jpg

    to see a script that you can run when you hit F5. This is an advanced form of the above tip. It allows you to specify a script to run when you hit F5. This is often used to set up the environment for a function that you are working on.

    These five tips will save you a few seconds each, hundreds of times a day. They will make MATLAB programming much faster and keep you in the flow!

6 CommentsOldest to Newest

Peter replied on : 1 of 6
Despite the "clear"-command I use "clear variables". That takes care of the workspace but does not touch the breakpoints. Peter
J.R.! replied on : 2 of 6
The "dangerous" use of clear is
clear all
. If you use ONLY
clear
(I use this sentence in quite ALL my scripts), only the current variables on the actual workspace will be cleared. That means, if you call
clear
inside a function, all the variables of the actual scope will be cleared, but your scope outside this function will stay untouched.
Matt Whitaker replied on : 3 of 6
Thanks Doug, I've bever noticed #5 before (Run configurations) . That looks extremely useful. Always a new trick to learn... Matt W.
Nelson replied on : 4 of 6
I think you have an error:
set(0,'DefaultFigureWindowStyle','undocked');
The figure property 'WindowStyle' has three documented settings: {normal} | modal | docked Instead of 'undocked', I think you want 'normal':
set(0,'DefaultFigureWindowStyle','normal');
Sean de Wolski replied on : 5 of 6
In addition to (5), I highly recommend using cell mode and creating a lot of cells. You can then ensure everything in the current cell is running as expected before continuing on to the next cell. The cell mode "evaluate current cell" button works just as quickly as play and can be used inside a function with sample variables if necessary - much easier than using breakpoints and repeatedly calling the function.
alex replied on : 6 of 6
I do not use these shortcuts on desktop because using the mouse is slow. It would really save me much time having "close all" with a key shortcut. Unfortunately it can be done when the cursor is on the editor or command window, but not when you are elsewhere. Check this shortcut of mine: function s(varargin)%function varargout=s(varargin) % S Equivalent to SIZE, but you do not need the brackets % Examples: use S X in the place of SIZE(X), or also S X Y ... % instead of SIZE(X),SIZE(Y) ... for i=1:length(varargin) [varargout{i,1:length(varargin)}]=evalin('caller',['size(',varargin{i},')']) ; [varargout{i,:}] end