Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
MATLAB coding means writing sequences of commands and function calls, saved as scripts or functions, that operate on arrays and matrices. This guide is for researchers and analysts who need to go from the command window to a working, reusable program: what the building blocks are, how to learn them, how MATLAB’s language compares with Python, and where Model-Based Design fits.
What MATLAB coding is, and what it is used for in research
MathWorks describes the simplest type of MATLAB program as a script: a sequence of commands and function calls saved in a file. MATLAB “operates primarily on arrays and matrices, both in whole and in part,” so most code expresses calculations on whole arrays rather than element-by-element loops (MathWorks, Getting Started with MATLAB, R2026b documentation, read 2026-11-08). For the research tasks the language is used for, see what MATLAB is used for; for array addressing, see MATLAB indexing and matrices in MATLAB.
What programming language does MATLAB use?
MATLAB has its own language, called the MATLAB language. MathWorks calls it “the first (and often only) programming language for many engineers and scientists” and describes MATLAB as a computing platform, whereas Python is “a general-purpose programming language” (MathWorks, MATLAB vs. Python, read 2026-11-08).
Install or access MATLAB
Get MATLAB from MathWorks. MATLAB is not free, but many people get access through a school or employer (MathWorks, MATLAB vs. Python). Installation steps are covered in installing MATLAB and licence types in MATLAB licensing.
How can I learn MATLAB code?
MathWorks lists these official getting-started topics: Matrices and Arrays, Array Indexing, Workspace Variables, Text and Characters, Desktop Basics, 2-D and 3-D Plots, and Help and Documentation. MATLAB Onramp is described by MathWorks as a free two-hour online MATLAB course. A practical order is: Onramp, then Desktop Basics and Matrices and Arrays, then the first script below, then functions.
Step-by-step: write and run a first script, then a function
- In MATLAB, create a new file and save it as
mystats.m. A file of commands with no function definition is a script. - Type commands that assign variables and call a function. To call a function, enclose its input arguments in parentheses. For example:
x = 3;andy = 2;. - Define a function with the lowercase keyword
function, an output name, a name and input arguments, thenend. MathWorks’ example computes a factorial:function f = fact(n) f = prod(1:n); end - If the function lives in its own file, give the file the same name as the function (
fact.m). MathWorks states that MATLAB associates your program with the file name, not the function name. - Call it from the command window:
x = 5; y = fact(5). - To keep everything in one script, put local functions after the script code. MathWorks’ example
mystats.m:x = 3; y = 2; z = perm(x,y) function p = perm(n,r) p = fact(n)/fact(n-r); end function f = fact(n) f = prod(1:n); end - Run it by typing
mystatsin the command window.
Source: MathWorks, “Create Functions in Files,” MATLAB documentation, read 2026-11-08.
Script, function or live script: which one are you writing?
| Type | What it is | Arguments | Workspace |
|---|---|---|---|
| Script | Stores commands exactly as you would type them at the command line | None; edit the values in the file | Base workspace |
| Function | Declared with the function keyword, for example function a = triarea(b,h), and called by name |
Input and output arguments | Its own workspace, separate from the base workspace |
| Live script | A script opened in the Live Editor, where output appears next to the code | None; it is a script | Base workspace, as for any script |
Source: MathWorks, Scripts and Functions, and Create Live Scripts.
The MathWorks triarea example shows the difference. Run as a script, it reads b and h from the file and stores the result in a in the base workspace, so each run overwrites a and you edit the file to change the inputs. Written as a function, triarea(b,h) takes the base and height as inputs, so calls such as a1 = triarea(1,5) and a2 = triarea(2,10) store results in separate variables and leave the base-workspace a unchanged. If you want code to take different inputs each time, write a function; see how to declare a function in MATLAB.
Other ways to create and run a script
Any one of these routes creates a script file:
- Select commands in the Command History, right-click, and choose Create Script.
- On the Home tab, click New Script.
- At the command line, type
editfollowed by a file name, for exampleedit numGenerator. This creates the file if it does not exist. Typingeditalone opens a file called Untitled.
To run a script, type its name at the command prompt and press Enter, or click Run on the Editor tab. You can also run it from another code file by adding a line with the script name, such as numGenerator;; MATLAB executes the script when you run that file. Saving files and variables is covered separately in saving in MATLAB.
Create and run a live script
- On the Home tab, click New Live Script.
- To convert an existing script, right-click its document tab and select Open scriptName as Live Script. This creates a copy of the file and leaves the original untouched.
- To run one section, click the vertical striped bar to the left of that section. To run all the code, click Run on the Live Editor tab. You do not need to save a live script to run it.
- To save, click Save on the Live Editor tab or press Ctrl+S.
Source: MathWorks, Create Live Scripts.
Output and how to read it
A statement with no trailing semicolon prints its result. y = fact(5) prints y = followed by 120, and running mystats prints z = followed by 6 (MathWorks documentation examples). A trailing semicolon suppresses printing; the variable is still stored and appears in the workspace.
In a live script, output appears to the right of the code by default. On the View tab you can choose Output on Right, Output Inline (each output appears underneath the line that creates it) or Hide Code, and you can clear results with Clear All Output. For a plain script, results go to the base workspace and the Command Window.
Troubleshooting
- A function is not found or the wrong one runs: the function name and file name should match, because MATLAB associates the program with the file name.
- A local function in a script errors in an older release: MathWorks notes that before R2024a, local functions in scripts must be defined at the end of the file, after the last line of script code. Place them there.
- The keyword is not recognised:
functionmust be lowercase. - An error in a live script: the status indicator changes to an error icon next to the line of code, and the error message appears as output. Click the icon to go to the error.
- A script needs different values: a script takes no arguments, so edit the values in the file, or move the code into a function.
How does MATLAB compare to Python?
MathWorks states that Python is a general-purpose language while MATLAB is a computing platform for engineering and science; that Python and most of its libraries are free while MATLAB is not; that MATLAB licences include live support while Python relies on community support; and that it recommends using both together. MathWorks reports more than 5 million MATLAB users (a vendor figure). These are the vendor’s statements; weigh them accordingly.
How can I use Model-Based Design in MATLAB?
MathWorks defines Model-Based Design as “the systematic use of models throughout the development process.” Its four capabilities are modeling and simulation, automation, a digital thread (traceability from requirements to testing), and operation (digital twins). The core tools are MATLAB and Simulink, and MathWorks recommends adopting it in stages, starting with one project (MathWorks, Model-Based Design, read 2026-11-08). Simulink is a block diagram environment for multi-domain simulation, and Simulink Coder can generate embedded C code; see MATLAB and Simulink.
Free alternatives
| Option | What the source says | Source |
|---|---|---|
| GNU Octave | A high-level language primarily intended for numerical computations, such as linear and nonlinear equations, numerical linear algebra and statistical analysis; freely redistributable under the GNU General Public License. | GNU Octave documentation, read 2026-11-08 |
| Python | A general-purpose language; Python and most of its libraries are free. | MathWorks, MATLAB vs. Python |
| NumPy (Python) | Describes itself as the fundamental package for scientific computing with Python; distributed under a BSD licence. | numpy.org |
| R | A language and environment for statistical computing and graphics; free software under the GNU General Public License. | r-project.org |
| Julia | Dynamically typed and designed for high performance; programs compile to native code via LLVM; MIT licence. | julialang.org |
Of these, only GNU Octave states MATLAB compatibility: its project site says its syntax is largely compatible with Matlab and that it is drop-in compatible with many Matlab scripts, not all of them. The others are different languages, so code is not interchangeable. Test any script you plan to move before relying on its results.
FAQ
Can I put MATLAB on my resume?
Yes, there is no rule against it. The useful approach is to name what you built or analysed with it (for example scripts, functions or Simulink models), because a bare skill name shows less than a described outcome. This is general advice, not a sourced policy.
How do I write my first MATLAB code?
Click New Script on the Home tab, type the commands you would normally enter at the command line, save the file, and click Run on the Editor tab.
What is the difference between a script and a function in MATLAB?
A script stores commands as typed at the command line and uses the base workspace, with no arguments. A function is declared with the function keyword, takes input and output arguments, and has its own workspace.
Can I run MATLAB code from another file?
Yes. Call the script by name in a line of the other file, for example numGenerator;; MATLAB runs the script when you run that file.
Is MATLAB coding the same as Python coding?
No. They are different languages with different design goals, as described above, and MathWorks recommends using them together.
Sources
- MathWorks: Getting Started with MATLAB (read 2026-11-08)
- MathWorks: Create Functions in Files (read 2026-11-08)
- MathWorks: MATLAB vs. Python (read 2026-11-08)
- MathWorks: Model-Based Design (read 2026-11-08)
- GNU Octave documentation: Introduction (read 2026-11-08)
- MathWorks: Create Scripts (read 2026-11-14)
- MathWorks: Scripts and Functions (read 2026-11-14)
- MathWorks: Create Live Scripts (read 2026-11-14)
- Project sites: GNU Octave, NumPy, The R Project, Julia (read 2026-11-14)







