MATLAB

Programming with MATLAB

MATLAB is a high-level programming language designed for engineers and scientists that expresses matrix and array mathematics directly. You can use MATLAB for everything, from running simple interactive commands to developing large-scale applications.

Start Simple—No Programming Experience Required

Get started quickly by executing commands interactively with immediate results.

>> sqrt(42)
ans =
    6.4807

You can express matrix and array mathematics directly using familiar syntax.

>> A = [7 8 2; 3 2 6; 5 9 4]
A =
     7     8     2
     3     2     6
     5     9     4

MATLAB® provides thousands of built-in functions for common mathematical, scientific, and engineering calculations.

>> B = eig(A)
B =
   14.9016
    2.3000
   -4.2015

You can choose from a variety of built-in plots to visualize your data. Specialized data types, including numeric, string, datetime, categorical, structures, and tables are available to represent your data. You can perform common tasks using functions that are specifically designed for each data type.

>> stars = readtable('StarTypes.xlsx',TextType = 'String'); 
>> stars.Class = categorical(stars.Class) 
stars = 
      Class   Temp         Color        Fraction
      _____   _____    _____________    ________

        O     30000    "Blue"              3e-05 
        B     10000    "Blue White"       0.0013 
        A      7500    "White"             0.006 
        F      6000    "Yellow White"       0.03 
        G      5200    "Yellow"            0.076 
        K      3700    "Light Orange"      0.121 
        M      2400    "Orange Red"       0.7645

With MATLAB, you can develop algorithms much faster than in traditional languages, such as C, C++, or Fortran, without having to declare variables, allocate memory, or compile code.


Learn more

Combine Commands into a Script

You can combine commands to create scripts that enable you to automate your work. Then add high-level programming constructs such as conditional statements and loops. You can run your script as an entire program or divide it into sections that can be run individually. With the Live Editor, you can create a script that is an executable notebook with output and visualizations next to the code that produced them. Use formatted text, headings, equations, images, and hyperlinks to document your work then share it with others.

Combine commands to create a script (left). Use the Live Editor to create an executable notebook (right).

Combine commands to create a script (left). Use the Live Editor to create an executable notebook (right).


Write Reusable Functions

You can use functions to break down a complicated program into smaller, reusable parts. You can automatically refactor code in scripts into reusable functions. Functions can have optional, named arguments to make them easier to use. Function argument validation eliminates the need to write complex input error checking code. You can use language features that enable your functions to manage and recover from errors.


Write functions with multiple inputs and outputs. Use function argument validation to make functions more robust

Write functions with multiple inputs and outputs (top). Use function argument validation to make functions more robust (bottom).

Example Transmitter classes in a wireless communications application.

Use object-oriented programming to model real-world objects and manage software complexity.

Author Custom Classes

With object-oriented programming, you can define objects that combine data (properties) with functions that operate on that data (methods). You can use objects to model the behavior of devices and systems in the real world and organize code into components that are easier to maintain and extend.

A MATLAB class defines a set of instructions used to build a specific type of object. Classes contain information about:

  • Properties to store data for each object of the class
  • Methods that define the operations that can be performed on each object of the class
  • How class properties and methods behave and how they are accessed from outside the object
  • Superclass and subclass relationships between classes through inheritance

Develop Large-Scale Applications

You can develop and maintain large-scale applications using MATLAB with projects, source control integration, unit testing, continuous integration and deployment, and toolbox packaging. To learn more, see Software Development Tools.