Check whether the input is a valid variable name

To check whether the input is a valid variable name isvarname( ) is used. This function determines if the input is a valid variable name. If it is a valid MATLAB variable name the isvarname function returns logical 1 (true). Otherwise, it returns logical 0 (false).

Syntax:

t = isvarname(s)

isvarname s

Example 1:

Matlab




% MATLAB code for isvarname
isvarname Number_1


Output:

ans = logical
    1

Example 2:

Matlab




% MATLAB code for isvarname()
Test= isvarname(Digit_1)


Output:

Test = logical
      1

Variable Names in MATLAB

A variable is a named-memory location that stores different types of data which can be used to perform a specific set of operations. It can be thought of as a container that holds some value in memory. The Matlab workspace store all the variables being used during a session. This workspace not only deals with the creation of new variables but also supports the reusing of existing variables in the execution of the command. Each variable in the Matlab environment is treated as a matrix or an array of different data types. In Matlab, variables are assigned using the assignment ‘=’ operator. 

Note:

  • After the creation of a variable, we can use it later in our program.
  • There must have values assigned to variables before they are used.
  • If an expression returns a result without being assigned to any variable, the system implicitly assigns and stores the value to a special variable named ‘ans’. But ans variable is specific to the current workspace and the value of ans can change frequently, that is why the use of ans in a script or function is not recommended.

Example 1: 

Matlab




% MATLAB code for defining a and initializing 
% it with a value 10 using assignment operator '='
a = 10


Output:

a = 10

Example 2:

Matlab




% MATLAB code for calculates an expression
% and stores value 13 in variable b
b = sqrt(169)


Output:

b = 13

Example 3:

Matlab




% MATLAB code to stores the expression 
% in special variable 'ans'
sqrt(169)


Output:

ans = 13

Similar Reads

Rules for creating a variable:

...

Check whether a variable name is a Matlab keyword:

...

Check whether the input is a valid variable name:

...

Conflicts between variable and function name:

A variable name is the name of the memory location where we can store our values. Some of the rules for naming a variable are:...