0% found this document useful (0 votes)
5 views

Week 1 Part 4 Fundamentals

The document provides an introduction to programming in MATLAB, covering key concepts such as assigning values to variables, entering commands, and utilizing the command history. It explains how to manage variables in the base workspace and introduces MATLAB Online as an alternative to local installation. Additionally, it highlights the importance of case sensitivity in variable names and offers tips for using the Command Window effectively.

Uploaded by

Ngọc Vi Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week 1 Part 4 Fundamentals

The document provides an introduction to programming in MATLAB, covering key concepts such as assigning values to variables, entering commands, and utilizing the command history. It explains how to manage variables in the base workspace and introduces MATLAB Online as an alternative to local installation. Additionally, it highlights the importance of case sensitivity in variable names and offers tips for using the Command Window effectively.

Uploaded by

Ngọc Vi Cao
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Fundamentals

Where do I find MATLAB? Here!

Do I have to have MATLAB installed on my computer right


now? No, you can use MATLAB Online!

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 1


Table of Contents

What we've covered this week in Part 3Variables and Commands


Assigning Values to Variables Entering Commands
The Command History Variables in the Base Workspace
What we've covered this week in Part 4

MATLAB Live Script

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 2


What we've covered this week in Part 3
This week in Part 3 we learnt about:

 Data types

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 3


Variables and Commands
Assigning Values to Variables
You can assign values to a variable using the equals sign (=), which is the assignment
operator in MATLAB:

x = 6;

MATLAB computes the value of the expression on the right of the equals sign and then
assigns that value to the variable named on the left.
If the variable on the left already exists, it is overwritten with the new value.

The expression to the right of the equals sign can be anything that MATLAB can
evaluate:

x = 42;
y = x/6; % y has the value 7
x = 3.14; % y still has the value 7
y = y - 1; % y is now 6

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 4


Variables and Commands
When a computation is carried out but not explicitly assigned to a variable, MATLAB
assigns the output to the default variable ans.

Run the code using the play button and see the output:

6*7

Entering just the name of a variable, with no assignment or calculation, returns the
current value of that variable:
x
x=
3.1400

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 5


Variables and Commands
Run the code and see if the output matches the above:
x

Variables remain in memory until explicitly cleared:

clear x % deletes just x


clear % deletes all variables

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 6


Entering Commands
Enter commands at the MATLAB prompt (> >) in the Command Window. MATLAB
interprets and executes the command when you press the Enter key:
>> x = 42
x = 42
If the command generates text output, the output is shown in the Command Window.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 7


Entering Commands
A semicolon (;) at the end of a command suppresses any output, although the command
is still executed:

Enter the command below in the command window, do you see the output?

y = 3.14;

Note MATLAB is case sensitive: name is not the same as Name or NAME.

Enter the command below in the command window, do you see that there was an
additional variable “Y” that was created?

Y = 3.14;

This Y variable is different from the y variable we defined previously.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 8


Entering Commands
This Y variable is different from the y variable we defined previously.

Although the Command Window shows a record of the commands issued and the output
returned, this record is not editable: only the current line can be edited.

Several keyboard shortcuts are available to assist in entering commands:

Home, End Move to the beginning, end of an expression


↑, ↓ Scroll through previous commands
Esc Deletes the expression at the prompt
Tab Displays commands that complete a partial expression

Useful commands for managing the Command Window are:


clc Clears the Command Window
home Moves the prompt to the top of the Command Window (without
clearing commands)
© 2023 Introduction to Programming in MATLAB (1), Fundamentals 9
The Command History
MATLAB automatically keeps a history of the commands you type. These commands
remain from session to session until you choose to delete them.

Pressing the up-arrow key on your keyboard at the command prompt brings up the
Command History window. In the Command History you can:
• Scroll through commands with the up and down arrow keys.
• Select commands with the mouse.
• Select multiple commands (use Shift-click or Ctrl-click).
• Edit recalled commands.
• Re-enter commands.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 10


The Command History

If you partially enter a command, the Command History will be filtered to show only
commands that match the portion entered. Use the window action menu to adjust the
filtering options.
Later we’ll see how we can use the Command History to recall commands and help
create scripts to automate your tasks.
© 2023 Introduction to Programming in MATLAB (1), Fundamentals 11
Variables in the Base Workspace
Commands executed in the command window or in a Live Script are stored in the form of
MATLAB variables in the base workspace (MATLAB memory).
The contents of the base workspace are shown in the Workspace browser window. The
browser displays the names of the variables currently in memory and (optionally)
information about the variables (e.g., size and class).

You can rename a variable by right-clicking it in the Workspace browser and choosing
Rename from the context menu.
Right-clicking on the Name button at the top of the Workspace browser displays a context
menu from which you can choose which information to display about the variables.

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 12


What we've covered this week in Part 4
This week in Part 4 we learnt about:
• Assigning values to variables
• Entering commands
• The command history
• Variables in the base workspace

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 13


MATLAB Live Script
Click on the link below for this lecture’s MATLAB live Script

• Week_1_Part_4_Fundamentals.mlx

© 2023 Introduction to Programming in MATLAB (1), Fundamentals 14

You might also like