Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Begin with Existing Code

Create M-Files from Command Window and History

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.

Use Existing M-Files and Examples

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.

MATLAB and Toolbox Functions

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.

Demos and Examples

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

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".

  


Recommended Products

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