Real Technology Tools

Getting Started With MATLAB: Create, Run Scripts, Define Variables

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

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:

The Current Folder in MATLAB

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”:

  3. In “Initial working folder” you can toggle the third option, which is “Specify the full path to a folder”:
    You can then specify your initial working folder.

MATLAB: Define a Variable

How to Define a Variable 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:

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:

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:

Exit mobile version