MATLAB Programming/Variables

From Wikibooks, open books for an open world
< MATLAB Programming
Jump to navigation Jump to search


Variables are just a container of your data. You can give any name to your variable as long as it is under 63 characters, starts with an alphabet and is without any punctuation in between. The reason for not allowing any punctuation is that the punctuation marks like . , : , ; etc have a special meaning in MATLAB and can alter the flow of execution


is a perfectly valid variable name. There may be some variables names that are not allowed even if they are perfectly valid variable names, because they are reserved keywords and are used to notify some code execution logic. However their number is limited and meaningful names for variables can be created outside this pool of names. To find if any word is a reserved keyword, run this check on the command line.


The result would be

   ans =

All values of variables are referred by their current instance in the workspace. The variables can be overwritten during the execution of MATLAB scripts and the values vary accordingly. All operations use the current value in the workspace.

Let's try something good. We all know that the value of pi is 3.1416, lets ask MATLAB the same, type this on the command window


The result is

   ans =

Lets try overwriting it...

   pi = 0

...and see it once again.

   ans =

Calculate the area of a circle now.

   Area = pi * 10 * 10

To keep execution clean, a semicolon (;) is used to suppress output.

 A = 10;