| MATLAB® | ![]() |
In the Editor, to run a script M-file, or a function M-file
that requires no input arguments, click the Run button
on the toolbar. The button's ToolTip
includes the name of the file to be run, which is useful when you
have multiple files open. Alternatively, select Debug > Run file name.
If the file is neither in a directory on the search path nor in the current directory, a dialog box appears with options that allow you to run the file. You can either change the current directory to the directory containing the file, or you can add the directory containing the file to the search path.
If the file has unsaved changes, running it from the Editor automatically saves the changes before running. In that event, the Debug menu item is Save File and Run file name.
If the M-file is a script, you can view the value of a variable in the file, which is called a data tip (like a ToolTip for data). You need to set the preference to show data tips in edit mode—select File > Preferences > Display, and for General Display Options, select the check box for Enable datatips in edit mode.
In the Editor, you can provide values for a function's input arguments using a run configuration, and then run that configuration to use the assigned values. When you are editing a function M-file, use a run configuration as an alternative to running the function in the MATLAB® Command Window. You can associate multiple run configurations with an M-file to assign different input values. MATLAB saves the run configurations between sessions to a file named run_configurations.m. (See About the run_configurations.m File for details.)
Consider the function collatzplot_new.m, which computes and plots the Collatz sequence for any given positive integer. This function requires you to specify the integer as an input value. You cannot simply run collatplot_new.m in the Editor because the input value is not defined. One way to specify the input value is to run the M-file in the Command Window. Run configurations allow you to run collatzplot_new(specific value) in the Editor.
You can also use run configurations to provide preparatory or setup information before running an M-file, whether it takes input arguments or not.
Note M-File run configurations use the base MATLAB workspace. Therefore, a value that you assign to a variable in an M-file run configuration overwrites the value for that variable (assuming it currently exists) in the base workspace. |
Follow these steps to create and use a run configuration for an M-file in the Editor. These steps specify Editor toolbar buttons, but you can also use equivalent options in the Debug menu.
Open the file you want to run in the Editor. For example, open collatzplot_new.m by running the following command:
edit(fullfile(matlabroot,'help','techdoc',... 'matlab_env','examples','collatzplot_new'))
To work with collatzplot_new.m on your system, save the file to a directory for which you have write permission. In the example, the file is saved to I:\my_matlab_files\my_mfiles\collatzplot_new.m.
Click the down arrow on the Run button in the Editor toolbar,

and then select Edit Run Configurations for file-name, where file-name in this example is collatzplot_new.m.

The Edit M-File Configurations dialog box opens, with a default run configuration template for collatzplot_new.m.

In the MATLAB expression area of the dialog box, enter MATLAB statements that you want to run. Delete the existing comments or replace them with comments relevant to your run configuration. To undo and redo, use the keyboard shortcuts for your platform, such as Ctrl+Z and Ctrl+Y for Microsoft®Windows® platforms.
In this example, set m equal to 3, which is a small value useful for debugging purposes. Complete the statement to run collatzplot_new(m).

The MATLAB expression area provides syntax highlighting and shows M-Lint messages, similar to the Editor.
To ensure your run configuration executes as expected, click Run to execute the statements in the MATLAB expression field. In this example, collatzplot_new(3) runs, and a Figure window displays the plot.

You can modify the statements in the MATLAB expression area of the dialog box and click Run to see the results of the changes. You can also modify the M-file and save the changes while the Edit M-File Configurations dialog box is open, and then click Run to see the results of the M-file changes.
You can assign a name using the Run configuration name field in the Edit M-File Configurations dialog box. By default, the run configuration name is the M-file name. If you expect to create multiple run configurations for an M-file, assign each a name that helps you identify the configuration. In this example, name the run configuration collatzplot_new_test.
MATLAB automatically saves the run configuration and its association with the M-file in the run_configurations.m file in your preferences directory.
For more information, see About the run_configurations.m File.
To close the Edit M-File Configurations dialog box, click Close.
After creating a run configuration, you can view and use the configuration without opening the Edit M-File Configurations dialog box.
In the Editor toolbar, click the down arrow on the Run button
and position the pointer
on a run configuration name. The MATLAB desktop displays a ToolTip showing the
run configuration's MATLAB Expression so you
can see what will run.

To use the run configuration, select the run configuration name. MATLAB runs the expression you specified in the run configuration. For example, select collatzplot_new_test, and MATLAB runs collatzplot_new(3), as specified in step 3. You can modify the M-file, save it, and execute the run configuration from the toolbar to see the effects of the M-file changes.
You can create multiple run configurations for a given M-file, allowing you to run with different values for input arguments, each for a different purpose. Create a named run configuration for each purpose, all associated with the M-file. Then any time you open the M-file, choose and execute the run configuration you want. For example, for collatzplot_new(m) you might use three values for m and have three run configurations:
Small value, for example, 3, for debugging and testing
Realistic value, for example, 200 or more, for a specific project
Random value to observe changes
Open the Edit M-File Configurations dialog box, and then do the following:
Select the M-file to which you want to add a run configuration, or select a configuration associated with that M-file.
Click the Add button
(under the list of M-files
and configurations), and then click Run Configuration.
MATLAB creates a new default run configuration template, in this example, collatzplot_new.
The example shows collatzplot_new and its default expression, as well as one previously created run configuration associated with collatzplot_new.m, collatzplot_new_test.

In the Edit M-File Configurations dialog box, modify, run, and name the new run configurations as you did for the initial run configuration, collatplot_new_test, as described in Create and Use a Run Configuration for an M-File.
For example, rename collatzplot_new to collatzplot_new_largevalue, and replace the default template expression with:
m=200; collatzplot_new(m)
To create another run configuration, click the Add button
again, and then click Run Configuration. Rename collatzplot_new_2 to collatzplot_new_random and replace the default template expression with:
% Random value m=int16(rand*50); collatzplot_new(m) clear all
Select a run configuration in the listing to see and modify its expression, or to rename the configuration. Click the expanders next to an M-file name (+ and - icon on Windows platforms) to see or hide all the configurations associated with that M-file.
To get a quick view of the expression in a configuration, position the pointer on the name of a configuration without selecting it. In this example, collatzplot_new_largevalue is selected and you can edit its expression or name. The pointer is positioned on collatzplot_new_test and you can see the statements in it.

To close the Edit M-File Configurations dialog box, click Close. MATLAB saves the configurations and their associations with the M-file in the run_configurations.m file in your preferences directory.
For more information, see About the run_configurations.m File.
When you create one or more run configurations using the Edit M-File Configurations dialog box, the Editor creates or updates the run_configurations.m file in your preferences directory (the directory MATLAB returns when you run prefdir). This is a text file that you can view and use to evaluate M-files.
Although you can port this file from the preferences directory on one system to another, there can only be one run_configurations.m file on a system. Therefore, you should only do this if you have not already created configurations on the second system. In addition, because this file may contain references to file paths, you need to be sure the specified M-files and paths exist on the second system.
The MathWorks recommends that you do not update this file in the Editor or a text editor. Changes you make using tools other than the Edit M-File Configurations dialog box may be overwritten.
Each time you change a run configuration using the Edit M-File Configurations dialog box, MATLAB updates the run_configurations.m file as well as the publish_configurations.m file. See About the publish_configurations.m File for more information about that file.
Follow these steps to find run or publish configurations. (For information on publish configurations, see Producing Published Output from M-Files.)
Open any M-file in the Editor. For example, open the MATLAB function sin.
Open the Edit M-File Configurations dialog box. MATLAB automatically creates a default configuration for sin.m, if none exists.
In the left pane, MATLAB lists all configurations currently defined for sin.m.
Click the X icon to clear the filter field.
![]()
In the left pane, MATLAB lists all M-files containing configurations.

Type a term in the filter field
to find an M-file or configuration
by name.
MATLAB displays only those M-files whose names contain the term, or whose associated configurations contain the term in their name. As you type, MATLAB filters out files and configurations that do not contain the term.
For example, type rand. In this example, only one M-file, collatzplot_new.m, has a configuration that contains the term rand.

If you cannot view the entire name of a configuration, drag the separator bar to the right of the list, making the left pane wider.

To see the expression in that configuration, select the configuration, or position the pointer over the name.
As you type additional letters in the filter field, fewer M-files remain in the list of results. Use the backspace key to modify the term. If there are no M-files or configurations containing the term, the list is empty.
If you no longer need a run or publish configuration because you do not use it or because you deleted the M-file with which it is associated, it is a good practice to delete the configuration. (For information on publish configurations, see Producing Published Output from M-Files.)
Open any M-file in the Editor.
Open the Edit M-File Configurations dialog box.
Do one of the following in the panel on the left:
If you want to remove a single configuration, select that configuration.
If you want to remove all the run and publish configurations for an M-file, select the M-file
Click the Remove button
.
To undo the last deletion, click the Undo button
. You cannot undo the last deletion
after you close this dialog box.
Each run and publish configuration is associated with a specific M-file. If you move or rename an M-file that has configurations, you need to redefine the association. If you delete an M-file, you might want to delete the associated configurations, or associate them with a different M-file. You might also need to modify the statements in the configurations so they will run.
When MATLAB cannot associate a configuration with an M-file, the Edit M-File Configurations dialog box displays the M-file name in red, displays a File Not Found message, and allows you to find the M-file to which you want to associate the configuration. In this example, MATLAB cannot find the file collatzplot_new.m, which has three configurations associated with it. For this example, collatzplot_new.m had been renamed to collatzplot_fixed.m, so the configurations associated with collatzplot_new.m need to be reassociated with collatzplot_fixed.m.

To reassociate a configuration:
In the list of configurations (left pane), select the M-file. The Associated M-file displays the full path to the M-file that was associated with the configurations. Click Choose.
In the resulting Open dialog box, navigate to and select the M-file with which you now want to reassociate the configurations. Click Open.
In this example, you want to reassociate the configurations with collatzplot_fixed.m; select collatzplot_fixed.m, and then click Open.
In the Edit M-File Configurations dialog box, the Associated M-file value reflects the change you made and the File Not Found message no longer appears.

You might want to rename the configurations to be consistent with the new M-file name, or at least to not reflect the former M-file name. This is not required, but it is a good practice. To do so, select a configuration from the list in the left pane. In the right pane, edit the value for the configuration name. Depending on the type of configuration that you are renaming, the field is labeled either Run configuration name or Publish configuration name. Repeat this step for all run and publish configurations associated with the M-file.
In this example, remove collatzplot_new from the start of each run configuration name.

For an M-file name change, you might need to modify the configuration statements to run correctly. For this example, modify the collatzplot_new(m) statement in each configuration to use collatzplot(m).

See Running an M-File with Breakpoints for additional information about running M-files while debugging.
While debugging, you can execute sections of an M-file even though there are changes. See Running Sections in M-Files That Have Unsaved Changes.
You can execute M-files one section at a time and quickly modify values incrementally using the toolbar. For more information, see Using Cells for Rapid Code Iteration and Publishing Results.
![]() | Saving, Printing, and Closing Files in the Editor | Finding Errors, Debugging, and Correcting M-Files | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |