MATLAB Fast Automation: Step-by-step Action Plan to Automate Your Work With MATLAB. (Grab The Preface and The 1st Chapter for FREE)

       

In this article, we’ll see the first things to learn getting started with MATLAB:

  • How to make MATLAB more personal
  • What the different windows are for
  • How to define a variable
  • How to create and run a script
  • The miscellaneous commands that are useful when you start

Making MATLAB More Personal

The first thing I like to do when I start learning a new programming environment is to change the layout to make it more personal. In MATLAB, you can do that easily by choosing among the predefined layouts:

  1. Go to the HOME tab
  2. “Layout” > “SELECT LAYOUT”

matlab layout tab
Then, you can choose a predefined layout:

Predefined MATLAB layouts

  • Modifying a layout: you can modify a predefined layout by left-clicking on a window and maintaining and sliding the window to your desired location.
  • Dock/undock a window: if you need to undock a specific window, you can do that by clicking on the button on the upper right of every window:Dock and undock a MATLAB windowAnd then clicking on “Undock” on the dropdown menu. To go back to the way it was, click on the same button on the undocked window and “Dock” on the dropdown menu.
  • Saving a modified layout: you can also save a layout by clicking on the “LAYOUT” dropdown menu and “Save Layout…”
    Once your layout is saved, you can find it in the “LAYOUT” dropdown menu.

The Current Folder in MATLAB

  • Definition:
    The current folder is the folder that MATLAB has access tomeaning that if you define a script of a function in a.m file and you save the file in that location, then you will be able to use it.
  • Changing your current folder:
    You can change folders by clicking on the “up one level” button
    :Moving up one levelAs the name indicates, it’ll move your current folder a level higher than your current location.
  • Adding a file to your current folder:
    If you want to add a file to your current folder, you can move your mouse to the current folder window and complete the following steps:

    Right click > “New File” > “Script”
    This will create a script in your current folder. We’ll see the different ways of creating a script later on in this article.

Changing Your Startup Folder

If you want to modify the current folder when MATLAB starts so that you don’t have to move your current folder every time you open it, do the following:

  1. In the “HOME” tab, click on “Preferences”
  2. Click on “General”:
    MATLAB Preferences: start up folder
  3. In “Initial working folder” you can toggle the third option, which is “Specify the full path to a folder”:Specifying the full path for your initial working folder
    You can then specify your initial working folder.

 

MATLAB: Define a Variable

How to Define a Variable in MATLAB

  • Default variable:
    First, let’s see the default variable in MATLAB. To do that, type 4 in your Command Window:Ans variable exampleIf you don’t specify anything, MATLAB defines the “ans” variable (which also appears in your workspace).
  • Specific variable:
    Now, let’s define a variable by specifying its name:
    Example of defining a variable in the WorkspaceThe value is the same as the “ans” variable, but the name is different. If you don’t want the definition of the variable to be displayed in the Command Window, use the semi-colon at the end of the definition:

    x = 4; % the definition of x will not be displayed

    Notice that all the commands that you’ve typed in the Command Window can be found in the Command History:

    Command History window in MATLAB

MATLAB: Whos

You have now defined 2 variables, “ans” and “x.” In order to see them in your Command Window, you can use the MATLAB command whos:

Example of the whos MATLAB command

MATLAB Script

How to Create a Script in MATLAB

There are 3 main ways to create a script in MATLAB:

  1. Click on “New Script” in the Home tab
  2. Right-click in the current folder > “New File” > “Script”
  3. Select lines of code in the “Command History” (by holding the CTRL key and left-clicking on the commands from your Command History) > right-click > “Create Script”

 

Run a MATLAB Script

There are 3 main ways to run a MATLAB script:

  1. Use a keyboard shortcut: F5
    This is by far the most efficient way in my opinion.
  2. Run a MATLAB script from a command line: write its name
    Write the name of your script in the Command Window to run it. (Make sure that the file is in your current folder or that the folder in which the file is located has been added to your search path, see MATLAB & Simulink: Why It Doesn’t Find Your Function or Variable)
  3. Use the “Run” button:
    Run button for a MATLAB script

 

MATLAB: Clearing the Workspace and Other Useful Commands

The Fundamentals

  1. To clear the Workspace, use:

    clear all; % delete variables in the Workspace
  2. To clear the command window, use:

    clc; % clean up the variables in the Command Window

Useful to Know When You Start

  1. The up arrow key: use the up arrow key in your Command Window to find a command you used in the past. Every time you use the up arrow key you will go further back in the Command History.
  2. doc commandName: if you want information about a specific MATLAB command use:

    doc commandName % provide everything you want to know about commandName
  3. demo: use the demo MATLAB command to find tutorial and videos about different topics:

      demo % provide Mathworks tutorials

     

Key takeaways:

  1. Personalizing MATLAB:
    • changing your layout: “HOME” > “Layout”
    • saving current layout: “HOME” > “Layout” > “Save Layout”
    • dock/undock a window: window’s top right icon > “dock”/”undock”
  2. MATLAB Windows:
    • current folder: folder that MATLAB has access to
    • changing the startup folder: “HOME” > “Preferences” > “General”
  3. Useful commands:

    clc % clean up the command window
    clear all % get the previous command
  4. MATLAB Script:
    3 Ways to Create a Script:

    1. “HOME” > “New Script”
    2. Right-click in current folder > “New File” > “Script”
    3. Command History > select lines > right click > “Create Script”

    3 Ways to Run a Script:

    1. Use the keyboard shortcut “F5”
    2. MATLAB Editor > click on “Run”
    3. Write the name of the script in the command window:
      >> myScript

If you want to save time learning MATLAB, this article might also interest you: