| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Before you begin writing MATLAB code in a blank file, consider starting with existing resources for the code, and then use the MATLAB Editor to modify the code.
You create and run MATLAB statements in the Command Window, modify those statements to your satisfaction, and then create an M-file that includes the statements. To facilitate this process, in the Command History, select the MATLAB statements you want to include in the M-file. Right-click and select Create M-File. The Editor opens a new file that includes the statements you selected from the Command History. You can also copy the statements from the Command History and paste them into an existing M-file.
If you can find existing M-files that accomplish what you want to do, copy, and use the code in your own M-file, assuming you have legal permission to do so. Following are some resources you can use.
You can access and reuse the code in most MATLAB and toolbox functions that have a .m file extension. You cannot use MATLAB and toolbox functions that are built-in. They are efficient but their code is not accessible.
If there is a MATLAB function that is similar to what you need to do and it is not built-in, open the file in the Editor and use it as a basis for your file. Be sure to save the file using a different name and in a folder that is not in matlabroot/toolbox. See Saving M-Files for details.
The MATLAB product and its toolboxes include demonstration programs. You can view the code in the demos and copy it for use in your own M-files. To see the demos, type demo, which opens the Help browser to the Demos pane. For more information about demos, see Using Demos and Code Examples.
There are also code examples in the online documentation. To
see a list of examples for a product, type helpbrowser to
open the Help browser. In the Contents pane,
click + for a product to view the
help topics, and then select the
Examples entry.
File Exchange enables you to use files that were created by other users. To save time and get new ideas, consider seeing if someone has already provided a file similar to what you need before creating a new file. In addition, consider submitting files you create for others to use. For more information, see File Exchange — Finding and Getting Files Created by Other Users".
![]() | Editing and Debugging M-Files | Ways to Edit, Evaluate, and Debug M-Files | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |