Site icon TestingDocs.com

Variables in MATLAB Command Window

Introduction

In this tutorial, we will learn how to declare and use the variables in MATLAB command window prompt. Variables are used to store information. We can use the assignment operator (=) to store information.

Variables Assignment

Let’s use some variables and assign values to the them in the Command Window prompt.

Calculation using the variables.

>> a = 7;
>> b = 9;
>> ans = a*b

ans =

63

 

 

Variables a and b are assigned values. ‘a’ and ‘b’ are initialized to 7 and 9 values respectively. The product of and b is stored in ‘ans’ variable.

a = 7;

The statement declared the variable and stores the value 7 in the variable. We can reassign the variables if you want so.

The variable names and the current values will appear in the Current Workspace window.

Variable names

There are some rules for naming the variables in MATLAB.

 

If you use invalid variable names, MATLAB will throw an error message and a suggested correction. You can use this command if you want, or hit Esc button to delete the suggestion.

Did you mean:
Suggested command

 

>> invalid$ = 10;
invalid$ = 10;
↑
Error: Invalid text character. Check for unsupported symbol, 
invisible character, or pasting of non-ASCII
characters.

>> 10name = 'Hello';
10name = 'Hello';
↑
Error: Invalid expression. Check for missing multiplication operator, 
missing or unbalanced delimiters, or other syntax error. 
To construct matrices, use brackets instead of parentheses.

Did you mean:
>> name = 'Hello';

Example

>> radius = 5;
>> area = pi*radius^2

area =

78.5398

 

 

pi is the floating point number and the value of  . It is the ratio of circle’s circumference to the diameter.

Exit mobile version