\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Dec 04, 2025 | 1056 words | 11 min read

15.1.1. Materials#

Reading#

Follow the steps provided here to install MATLAB on your system.

Explore example programs in MATLAB:

Commenting between MATLAB and Python#

One of the main differences that will be addressed the fastest will be how comments work inside of MATLAB versus Python

In MATLAB, ‘%’ are used to make comment lines, whereas in Python, a ‘#’ is used.

MATLAB Commenting#

% This is a comment line in MATLAB
charArray = 'Hello'      % This is a comment after a command

Python Commenting#

# This is a comment line in Python
charArray = 'Hello'      # This is a comment after a command

Character and String Arrays in MATLAB vs Python#

Definitions#

  • In MATLAB:

    • A character array is a sequence of characters using single quotes '...'

    • A string array is a container for pieces of text using double quotes "..."

  • In Python, strings are immutable sequences of characters using either single or double quotes ('...' or "..."). There is no separate type for character arrays.

📌 A key difference between the two languages:

  • MATLAB indexing starts at 1

  • Python indexing starts at 0

This off-by-one difference can lead to subtle bugs when translating logic between the two environments.

String Literals Examples (MATLAB)#

charArray = 'Hello'      % Character array
stringArray = "Hello"    % String array

MATLAB string arrays (double quotes) behave more like Python strings and support more built-in methods.

Common Operations (MATLAB vs Python)#

Operation

MATLAB

Python

Indexing

x(i), x(end-i+1)

x[i], x[-i]

Slicing

x(i:j), extractBetween(x,i,j)

x[i:j]

Concatenation

x + y (string), [x, y] (char)

x + y

Repetition

repmat(x, 1, n)

x * n

Membership

contains(x, y)

y in x

Length

strlength(x), length(x)

len(x)

Iteration

for i = 1:strlength(x)

for c in x:

Comparison

strcmp(x,y), x == y, x > y

x == y, x > y

Search

strfind(x, y), startsWith(x, y)

x.find(y), x.startswith(y)

Count

count(x, y)

x.count(y)

Sort/Reverse

sort(x), reverse(x)

sorted(x), reversed(x)

String Methods (MATLAB)#

MATLAB has many methods similar to Python string methods:

  • lower(x), upper(x)

  • split(x), strtrim(x), deblank(x)

  • replace(x, old, new)

  • startsWith(x, prefix), endsWith(x, suffix)

  • isstrprop(x, 'alpha'), isstrprop(x, 'digit'), isstrprop(x, 'alphanum')

To convert between types in MATLAB:

x = convertCharsToStrings(y);  % Convert char array to string

MATLAB Indexing:

A = [10, 20, 30, 40, 50];
first_element = A(1); % Accesses the first element (10)
third_element = A(3); % Accesses the third element (30)
last_element = A(end); % Accesses the last element (50)

Python Indexing:

A = [10, 20, 30, 40, 50]
first_element = A[0] # Accesses the first element (10)
third_element = A[2] # Accesses the third element (30)
last_element = A[-1] # Accesses the last element (50)

Control Flow: for and while Loops#

Both MATLAB and Python offer robust control flow mechanisms, including for and while loops, which are essential for repetitive tasks. While their functionalities are similar, their syntax and typical use cases differ.

For Loops#

For loops are used for iterating over a sequence or a range of numbers.

MATLAB for loop syntax: MATLAB’s for loop typically iterates over a numerical range or the columns of an array.

% Iterate over a numerical range
for i = 1:5
    disp(i); % Displays 1, 2, 3, 4, 5
end

% Iterate over elements of an array (less common directly than Python)
myArray = [10, 20, 30];
for val = myArray
    disp(val); % Displays 10, 20, 30 (iterates column-wise)
end

Python for loop syntax: Python’s for loop iterates over any iterable object (lists, strings, tuples, etc.).

# Iterate over a numerical range using range()
for i in range(5): # Iterates from 0 up to (but not including) 5
    print(i) # Prints 0, 1, 2, 3, 4

# Iterate over elements of a list
my_list = [10, 20, 30]
for val in my_list:
    print(val) # Prints 10, 20, 30

# Iterate with index and value using enumerate()
for index, value in enumerate(my_list):
    print(f"Index: {index}, Value: {value}")

Key Difference for for loops: MATLAB’s for loop typically uses a numerical range that starts at 1, aligning with its 1-based indexing. Python’s range() function, on the other hand, starts at 0 by default, reflecting its 0-based indexing.

While Loops#

While loops execute a block of code as long as a specified condition is true.

MATLAB while loop syntax:

counter = 1;
while counter <= 5
    disp(counter);
    counter = counter + 1;
end

Python while loop syntax:

counter = 0 # Python's common starting point
while counter < 5:
    print(counter)
    counter += 1

Key Similarity for while loops: The fundamental logic of while loops is very similar across both languages: they continue executing as long as a boolean condition remains true. The primary difference lies in the syntax of the condition and the indentation for defining the loop’s body.

Videos#

MATLAB Interface Introduction#

Algebraic Operations in MATLAB#

Built-in Functions#

Variable Names#

Assigning Vectors and Matrices#

Array Indexing: Copying Values#

Array Indexing: Replacing Values#

Scripts#

The fprintf Command#

Conditionals#

Loops (while/for):#

Element-by-element operations#

Concatenating arrays#

Publish MATLAB script#

MATLAB in VS Code#

Follow the steps below to connect your MATLAB account to VS Code.

Note

Before you proceed, MATLAB (R2021b or later version) must be installed on your system. To install MATLAB, click here

  1. Open VS Code and install the MATLAB extension from the Extensions Marketplace.

    ../../_images/ma_ext_install.png

    Fig. 15.1 A representation of installing MATLAB extension on VS Code#

  2. Go to Settings in VS Code.

  3. Search for “MATLAB: Executable Path”.

  4. Set the path to your MATLAB executable. You may have to update the version of MATLAB appropriately.

    C:\Program Files\MATLAB\R2024a
    
    ../../_images/ma_ext_install2.png

    Fig. 15.2 A representation of adjusting settings for MATLAB extension on VS Code#

  5. Close and reopen VS Code to apply the changes.

  6. Open a new MATLAB terminal in VS Code.

  7. Run the following command to test the set up:

    fprintf("hello World")
    
    ../../_images/ma_ext_test.png

    Fig. 15.3 A representation of opening a new MATLAB terminal and testing a print function in MATLAB#

  8. If the test function does not run, try restarting VS Code. Then, check the bottom right corner of VS Code for a notification, prompting you to connect your MATLAB Online Account. Click OK.

    ../../_images/ma_ext_install3.png

    Fig. 15.4 A representation of connecting MATLAB Online account#

  9. Now, a new tab will be opened in your web browser. Log in with your MATLAB credentials.

    ../../_images/ma_ext_install4.png

    Fig. 15.5 A representation of connecting MATLAB Online account#

  10. Once logged in, enter your MATLAB version as shown in Step 4. You may have to update the version of MATLAB appropriately.

    ../../_images/ma_ext_install5.png

    Fig. 15.6 A representation of connecting MATLAB Online account#

  11. Go to Step 5 and repeat the process.

  1. Open VS Code and install the MATLAB extension from the Extensions Marketplace.

    ../../_images/ma_ext_install.png

    Fig. 15.7 A representation of installing MATLAB extension on VS Code#

  2. Go to Settings in VS Code.

  3. Search for “MATLAB: Executable Path”.

  4. Set the path to your MATLAB executable. You may have to update the version of MATLAB appropriately.

    /Applications/MATLAB_R2024a.app
    
    ../../_images/ma_ext_install2.png

    Fig. 15.8 A representation of adjusting settings for MATLAB extension on VS Code#

  5. Close and reopen VS Code to apply the changes.

  6. Open a new MATLAB terminal in VS Code.

  7. Run the following command to test the set up:

    fprintf("hello World")
    
    ../../_images/ma_ext_test.png

    Fig. 15.9 A representation of opening a new MATLAB terminal and testing a print function in MATLAB#

  8. If the test function does not run, try restarting VS Code. Then, check the bottom right corner of VS Code for a notification, prompting you to connect your MATLAB Online Account. Click OK.

    ../../_images/ma_ext_install3.png

    Fig. 15.10 A representation of connecting MATLAB Online account#

  9. Now, a new tab will be opened in your web browser. Log in with your MATLAB credentials.

    ../../_images/ma_ext_install4.png

    Fig. 15.11 A representation of connecting MATLAB Online account#

  10. Once logged in, enter your MATLAB version as shown in Step 4. You may have to update the version of MATLAB appropriately.

    ../../_images/ma_ext_install5.png

    Fig. 15.12 A representation of connecting MATLAB Online account#

  11. Go to Step 5 and repeat the process.

  1. Open VS Code and install the MATLAB extension from the Extensions Marketplace.

    ../../_images/ma_ext_install.png

    Fig. 15.13 A representation of installing MATLAB extension on VS Code#

  2. Go to Settings in VS Code.

  3. Search for “MATLAB: Executable Path”.

  4. Set the path to your MATLAB executable. You may have to update the version of MATLAB appropriately.

    /usr/local/MATLAB/R2024a
    
    ../../_images/ma_ext_install2.png

    Fig. 15.14 A representation of adjusting settings for MATLAB extension on VS Code#

  5. Close and reopen VS Code to apply the changes.

  6. Open a new MATLAB terminal in VS Code.

  7. Run the following command to test the set up:

    fprintf("hello World")
    
    ../../_images/ma_ext_test.png

    Fig. 15.15 A representation of opening a new MATLAB terminal and testing a print function in MATLAB#

  8. If the test function does not run, try restarting VS Code. Then, check the bottom right corner of VS Code for a notification, prompting you to connect your MATLAB Online Account. Click OK.

    ../../_images/ma_ext_install3.png

    Fig. 15.16 A representation of connecting MATLAB Online account#

  9. Now, a new tab will be opened in your web browser. Log in with your MATLAB credentials.

    ../../_images/ma_ext_install4.png

    Fig. 15.17 A representation of connecting MATLAB Online account#

  10. Once logged in, enter your MATLAB version as shown in Step 4. You may have to update the version of MATLAB appropriately.

    ../../_images/ma_ext_install5.png

    Fig. 15.18 A representation of connecting MATLAB Online account#

  11. Go to Step 5 and repeat the process.