Working with Variables and Arrays in MATLABThe structural unit of data in any MATLAB program is the array. An array is a collection of record values organized into rows and columns and known by a single name. Arrays can be allocated as either vectors or matrices. The term "vector" is generally used to define an array with only one dimension, while the term "matrix" is usually used to determine an array with two or more dimensions. The numbers of the row state the size of an array, and the numbers of the column in the array, with the numbers of the row, mentioned first. The total number of items in the array will be the product of the number of rows and the number of the column. For example, the size of the following arrays are: Individual items in an array are addressed by the array name followed by the row and column of the particular item. If the array is a row or column vectors, then only one subscripts are needed. For example, in the preceding array a(2,1) is 3 and c(2) = 2. A MATLAB variable is an area of memory, including an array, which is called by a customerspecified name. The content of the array may be used or modified at any time by containing its name in an appropriate MATLAB command. MATLAB variables name must start with a letter, followed by any sequence of letters, numbers, and the underscore (_) character. Only the first 63 characters are essential; if more than 63 are used, the remaining characters will be ignored. If two variables are stated with names that only differ in the 64th character, MATLAB will treat them as the same variable. Creating and Initializing Variables in MATLABMATLAB variables are automatically generated when they are initialized. There are three methods to initialize a variable in MATLAB:
Initializing Variables in Assignment StatementThe simplest method to initialize a variable is to assign it one or more value in an assignment statement. An assignment statement has the standard form var = expression; where var is the name of the variables and expression is a scalar constant, an array, or a combination of constants, other variables, and mathematical operations (+, , etc.). The value of the expression is computed using the standard rules of mathematics, and the resulting values are saved in the named variable. The semicolon at the last of the statement is optional. If the semicolon is absent, the values assigned to var will be echoed in the command window. If it is present, nothing will be shown in the Command Window, even though the assignment has appeared. Examples of initializing variables with assignment statements contain var = 40i; The first example generates a scalar variable of type double and saves the imaginary number 40i in it. The second example generates a scalar variable and saves the result of the expression var/5 in it. The third example shows that multiple assignment statements can be placed on a single line, supported that they are divided by semicolons or commas. The last example display that variables can also be initialized with arrays of data. Such arrays are build up using brackets ([]) and semicolons. All of the items of an array are listed in row order. In other words, the value in each row are recorded from left to the right, with the topmost row first, and the bottommost row last. The single value within a row are separated by blank spaces or commas, and the rows themselves are divided by semicolons or newlines. The following statements are all legal arrays that can be used to initialize a variable: The number of items in every row of an array must be the same, and the number of items in every column must be the same. An expression such as [1 2 3; 4 5]; is illegal because row 1 has three items, while row 2 has only two items. Initializing with BuiltIn FunctionsArrays can also be initialize using builtin MATLAB function. For example, the function zero can be used to generate an allzero array of any desired size. There are a various form of the zeros function. If the function has an individual scalar argument, it will develop a square array using the single arguments as both the number of rows and the number of columns. If the function has two scalar argument, the first arguments will be the number of rows, and the second arguments will be the number of the columns. Since the size function return two values including the number of row and column in an array, it can be combined with the zero function to create an array of zeros that is the same size of another array. Some examples using the zeros function follow: a = zeros(2); These statements generate the following arrays: Similarly, the ones function can be used to generate array including all ones, and the eye function can be used to generate arrays including identity matrices, in which all ondiagonal items are one, while all offdiagonal items are zero. Following the tables containing a list of standard MATLAB functions useful for initializing variables. MATLAB Functions Useful for Initializing Variables
Initializing Variables with Keyboard InputIt is also possible to prompt a customer and initialize a variable with a record that he or she type directly at the keyboard. This option enables a script file to prompt a customer for input data value while it is executing. The input function shows a prompt string in the Command Window and then waits for the user to type in a response. For example, consider the following statements: my_val = input('Enter an input value:'); When this function is executed, MATLAB prints out the string 'Enter an input value:,' and then waits for the client to respond. If the customer enters a single number, it may just be typed in. If the customer enter an array, it must be enclosed in brackets. In other case, whatever is typed will be stored in variable my_val when the return key is entered. If only the return key is entered, then an empty matrix will be generated and stored in the variable. If the input function contains the character 's' as a second argument, then the input data is returned to the customer as a character string. Thus, the statement » in1 = input('Enter data: ');
Next TopicWorkspace, Variables, and Functions
