User Tools

Site Tools


variables

This is an old revision of the document!


Variables

Unlike some other programming languages that require you to declare the class of a variable, Matlab is smart enough to figure it out. You can create an integer variable by assigning a variable to an integer. For example, in the command window type

x = 2*3

Not only will it give you the result of the calculation, but in the workspace window you should now see a variable named x with the value of 6.

In like manner, if you wanted to create a variable that was a string you would assign a variable to a string. Type the following code in the command window.

Y = ‘Hello World’

Notice how I only used single quotes instead of double. Matlab is smart enough to differentiate between a char and a string so the user only needs to ever use single quotes. To prove this, create another variable.

t = ‘l’

To see a list of all the variables declared type “who” in the command window. If you want to see information about a specific variable type “whos variable name”. Type in the command into the command window for every variable and see how Matlab classified your variables. Compare with the image below.

Matlab will tell you the name, size, bytes, class, and attributes of every variable. To understand the description for size you need to know that Matlab puts every variable in the form of a matrix. The variable t is a 1×1 matrix composed of 1 element. The variable y is composed of a 1×11 matrix of the class char. This indicates that the variable y is composed of 11 class char elements.

variables.1440864203.txt.gz · Last modified: 2015/08/29 10:03 by petersen