Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

MATLAB Coding: Scripts, Functions and a First Program

MATLAB coding basics: scripts, functions and arrays, a worked first program, a learning path, the Python comparison and Model-Based Design.

Written and maintained by CASRAI Editorial Board

Last updated

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

  1. In MATLAB, create a new file and save it as mystats.m. A file of commands with no function definition is a script.
  2. Type commands that assign variables and call a function. To call a function, enclose its input arguments in parentheses. For example: x = 3; and y = 2;.
  3. Define a function with the lowercase keyword function, an output name, a name and input arguments, then end. MathWorks’ example computes a factorial:
    function f = fact(n)
        f = prod(1:n);
    end
  4. 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.
  5. Call it from the command window: x = 5; y = fact(5).
  6. 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
  7. Run it by typing mystats in the command window.

Source: MathWorks, “Create Functions in Files,” MATLAB documentation, read 2026-11-08.

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.

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: function must be lowercase.

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

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.

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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Coding: Scripts, Functions and a First Program

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →