MATLAB Variable

After reading this MATLAB Variable topic, you will understand how to create and manipulate Variable, and you will understand how to assign and display variable data in MATLAB.

In MATLAB, an assignment statement automatically generates variable.

Assign the value to a Variable

An assignment statement used for assigning a value to a variable. The assignment statement generally having the symbol = which is known as the assignment operator.

Example:

The statement in MATLAB is given as

x=10;
fprintf(

Output:

10

Explanation:

  • x=10; is an assignment statement
  • x is variable.
  • 10 is the value assigned to variable x.
  • = is the assignment operator.
  • fprintf(‘

Assign string to Variable

An assignment statement used for assigning the string to a variable.

Example:

The statement in MATLAB is given by

x='Hello';
fprintf(

Output

Hello

Explanation:

  • x is variable.
  • = is the assignment operator.
  • Hello is the string assign to variable x.
  • fprintf(‘

Display Variable Data

In MATLB, fprintf statement is used to print a variable data.

Example:

The statement in MATLAB is given by

x=5;
fprintf('The x is

Output

The x is 5

Explanation:

  • x is variable.
  • = is the assignment operator.
  • fprintf(‘The x is

Assign multiple Variables

We can assign values to multiple variable in single statement.

Example:

The statement in MATLAB is given by

x = 10; y = 32; r = -12;
fprintf('The x is

Output

The x is 10 The y is 32 The r is -12

Explanation:

  • x, y, and z are variables.
  • 10, 32 and -12 are the values assign to variables x, y, and z respectively.
  • = is the assignment operator.
  • fprintf(‘The x is

Rules for Defining Variable Name

  • A variable name must start with a letter.
  • Variable name length limits up to 63 characters long.
  • A variable name can be made with letters, digits, and the underscore character.
  • Punctuation characters (e.g., period, comma, semicolon) are not allowed for defining variable names.
  • MATLAB is case sensitive: it discriminates between uppercase and lowercase letters. For example, AA, Aa, aA, and aa are the names of four different variables.
  • No spaces are allowed between characters.
  • MATLAB Built-in function cannot be used as variable name. (Built-in function like cos, sin, exp, sqrt, etc.).
  • Keywords cannot be used as variable name. Keywords like break, case, catch, class, continue, else, etc.

Leave a Comment

error: Content is protected !!