0
0
MATLABdata~5 mins

Variable creation and assignment in MATLAB

Choose your learning style9 modes available
Introduction

Variables let you store information to use later in your program. Assignment means giving a value to a variable.

When you want to save a number or text to use multiple times.
When you need to remember a result from a calculation.
When you want to change a value and keep track of it.
When you want to organize data with names instead of just numbers.
Syntax
MATLAB
variableName = value;
Variable names must start with a letter and can include letters, numbers, and underscores.
The semicolon at the end stops MATLAB from showing the value immediately.
Examples
This creates a variable named x and assigns it the number 5.
MATLAB
x = 5;
This creates a variable name and assigns it the text 'Alice'.
MATLAB
name = 'Alice';
Stores the value of pi approximately in piValue.
MATLAB
piValue = 3.1416;
Assigns to result the sum of x and 10.
MATLAB
result = x + 10;
Sample Program

This program creates two variables a and b, adds them, and shows the result.

MATLAB
a = 10;
b = 20;
sumVal = a + b;
disp(['Sum of a and b is: ' num2str(sumVal)])
OutputSuccess
Important Notes

Variable names are case-sensitive: a and A are different.

Use disp to show values on the screen.

Ending a line with a semicolon ; hides the output in the command window.

Summary

Variables store data with a name you choose.

Use = to assign values to variables.

Semicolons keep your workspace clean by hiding output.