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.

In many cases, 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 directory that is not in matlabroot/toolbox. See Saving 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 Demos in the Help Browser.

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

The MathWorks Web site features a user-contributed code library, from which you can download free M-files contributed by users and developers of MATLAB software, Simulink® software, and related products. To view the files available to download, go to the MATLAB Central File Exchange page on the MathWorks Web site, http://www.mathworks.com/matlabcentral/fileexchange/index.jsp, or access it via the Help > Web menu in any desktop component.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS