Main Content

MATLAB Code Examples

Standalone Examples

A standalone example is a readable version of a MATLAB® script or live script that shows how to accomplish a particular task. MATLAB and all MATLAB toolboxes include examples as part of the installed documentation. (Before release R2012b, these examples were called demos.)

To access examples, click Examples at the top of a documentation page. The Help browser displays the examples for the current product category.

MATLAB includes examples that demonstrate various functionalities. For instance, to view examples demonstrating plotting in MATLAB, navigate to the MATLAB > Graphics > 2-D and 3-D Plots category and click Examples at the top of the page.

Help browser showing the first three MATLAB Graphics 2-D and 3-D Plots examples. The Examples filter at the top of the page is circled.

To copy the example and supporting files onto your system and open the example, use the buttons at the top of each example:

  • When viewing an example in the Help browser — Click the button to open the example in MATLAB. For instance, if the example is a live script, click the Open Live Script button to open the example live script in MATLAB.

    Open Live Script button

  • When viewing an example in a web browser — Click the Copy Command button to copy the command that opens the example. Then, enter the copied command into the MATLAB Command Window. You also can open some examples directly in your web browser. For instance, if you are not logged into a MathWorks® account, you can open some examples in your web browser by clicking the Try This Example button.

    Try This Example button and Copy Command button

Once the example is open, run it by clicking Run . To run the example one section at a time and view the incremental results, select the first section and then step through the script by clicking Run and Advance .

Additional examples, created by members of the MATLAB community, are available at the File Exchange.

Inline Examples

The product documentation also includes inline code excerpts, such as examples on function pages like cos or plot. You can run inline code from the Help browser by selecting the code, right-clicking, and then selecting Evaluate Selection, as shown.

Inline example with a section of code highlighted, the context menu open, and the Evaluate Selection option in the menu selected

See Also

| |

Related Topics

External Websites