MATLAB Community

MATLAB, community & more

Running functions with input from the Editor in R2007b

Despite the lack-luster title of this entry, I assure you that this new feature will be well worth the read, as it addresses one of our most common user requests!

In R2007b, you’ll notice a fancy new run button to the left of the debug toolbar (see Figure 1 below). The new split-button has a green play icon imposed on a document. Pressing the left side (with the icon) runs the currently selected configuration (I’ll explain what a configuration is in a minute), while pressing the right side (the downward facing arrow) will show you all the configurations for the file being edited.

New MATLAB Editor Toolbar
Figure 1.

Let me note that if you want to run your file as you used to, simply press the run button. Now, if you’re a little curious about what these new configurations can offer you, read on!

Configurations are small pieces of MATLAB code associated with a particular M-file. There can be many configurations associated with a given file, which can be very convenient as we’ll see below. When you first press the run button’s drop down arrow, you’ll see two menu items: the default configuration name (which happens to be the file/function name) as well as an option to edit configurations for the file you’re currently working with in the MATLAB Editor. If you select Edit Configurations for [your file] you’ll see the Configuration Editor (Figure 2 below).

Configuration Editor
Figure 2.

Notice that by default, the name of the file you’re editing (which should also be the function name) is inserted into the default configuration as well as any subsequently created configurations. Running such a configuration would be identical to typing the file name at the command prompt.

This seems like such a complicated way to run a file, you say…ahh, but we haven’t arrived at the good part yet! Say your function (we’ll call it superFunction) takes an input argument. Previously in the MATLAB Editor, there would have been no way to run your function. You would have been forced to go to the command window and type superFunction(1234).

Now all you need to do is edit one of superFunction.m‘s configurations, adding the desired input. You can then run your configuration from the Configuration Editor, or go back to the MATLAB Editor and select your configuration from the drop down portion of the run button. The run button will remember which configuration was last run. Thus, subsequent presses of the run button (or F5) will run the last configuration you ran.

Creating multiple configurations allows you to easily change how you run a function. Each configuration can pass in different input arguments or it can call your function with a different number of input arguments (if your using varargin). You can even do some setup and/or cleanup in the configuration! If you had a surface you regularly created, you could have a configuration like this:

x = magic(20);
x = x .* 2;
title('Magic Surface');

and maybe another like this:

x = magic(50);
x = x .* 5;
title('Another Magic Surface');

Configurations are currently stored in a binary file, but we’re actively working on making their storage more transparent.

Configurations should help you stay focused on writing and testing your code…let us know what you think!

  • print


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.