\[ \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{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\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 % % 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} \]

Mar 14, 2025 | 964 words | 10 min read

14.1.1. Materials#

Reading#

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

Do some research into some examples in MATLAB.

Character and String Arrays#

Definition of Character and String Arrays in MATLAB#

A character array is a sequence of characters. A string array is a container for pieces of text. Refer to https://www.mathworks.com/help/MATLAB/characters-and-strings.html

  • Character arrays are created using single quotes ‘…’

  • String arrays are created using double quotes “…”

MATLAB string arrays have more built-in functionality and behave more like Python strings.

String literals in MATLAB#

A string literal is a sequence of characters enclosed in:

  • Single quotes (’…’) for character arrays

  • Double quotes (”…”) for string arrays

Examples:#

  • charArray = 'Hello' : Character array

  • stringArray = "Hello" : String array

String manipulation as an immutable sequence#

For strings x and y with indices i, j, k, the following operations are supported:

  • Indexing : x(i), x(end-i+1)

  • Slicing (Substring extraction) : extractBetween(x, i, j), x(i:j)

  • Concatenation : x + y (for string type), [x, y] (for char arrays)

  • Repetition : repmat(x, 1, n), where n is an integer

  • Membership test : contains(x, y), ~contains(x, y)

  • Length : strlength(x), length(x), min(x), max(x)

  • Iteration : for i = 1:strlength(x)

  • Comparison : x == y, strcmp(x, y), strcmpi(x, y), x < y, x > y

  • Searching : strfind(x, y), contains(x, y), startsWith(x, y), endsWith(x, y)

  • Counting : count(x, y)

  • Ordering : sort(x), reverse(x)

String methods in MATLAB#

For strings x and y, the following methods are supported:

  • lower(x) : Converts all characters to lowercase

  • upper(x) : Converts all characters to uppercase

  • split(x) : Splits a string into a list of substrings

  • strtrim(x) : Removes leading and trailing whitespace

  • deblank(x) : Removes trailing whitespace only

  • regexprep(x, '^\s+', '') : Removes leading whitespace

  • replace(x, old, new) : Replaces a substring with another substring

  • strjoin(list, x) : Joins a list of substrings into a single string

  • startsWith(x, prefix) : Checks if a string starts with a specified prefix

  • endsWith(x, suffix) : Checks if a string ends with a specified suffix

  • isstrprop(x, 'alpha') : Checks if all characters are alphabetic

  • isstrprop(x, 'digit') : Checks if all characters are digits

  • isstrprop(x, 'alphanum') : Checks if all characters are alphanumeric

  • all(isstrprop(x, 'lower')) : Checks if all characters are lowercase

  • all(isstrprop(x, 'upper')) : Checks if all characters are uppercase

  • all(isstrprop(x, 'wspace')) : Checks if all characters are whitespace

Notes:#

MATLAB string arrays (”…”) support more built-in methods than character arrays (’…’). If using character arrays, you may need to convert them:

  • x = convertCharsToStrings(y) : Convert character array to string

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#

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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.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. 14.18 A representation of connecting MATLAB Online account#

  11. Go to Step 5 and repeat the process.

Optional Resources#

Click here to know more about how to get help in MATLAB