Matlab_Help_session_1

advertisement
Matlab Help session 1 - March 19, 2007
1) 5 major windows:
 Command window: where you type your codes
 Launch pad: list toolboxes available
 Current directory: list the files in the current directory
 Command history: list the codes that you have typed
 Workspace: list variable names + size + bytes + data type
2) File types:
 *.m: scripts (or file containing lines of codes), can be opened as text file
 *.mat: matlab data file, encrypted
 *.txt (or other data type): text data files must be stored as matrix format otherwise matlab cannot use them.
Note: Make sure that you are at the correct folder containing your script (code) or data
Note: Make sure that you load the correct file type.
To show the list of files:
>>ls
To show the list of variables already loaded onto workspace
>>whos
To load a file containing the data into workspace
>> load filename.txt
To save a variable to a text file
>> save filename.txt variablename -ASCII
3) Matrix and arithmetic:
Note: The most common mistake is to have mismatch in matrix dimension. You may check the workspace for the
size (or dimension) of your variables.
Matrices have size [rows by columns], commands such as colon ( : ) is used to denote the whole column or row.
Example, A(:, 3) will give you a column array denoting the 3rd column of A.
Some of the confusing numeric operations are:
 * vs .*
 ^ vs .^
 / vs ./
Miscellaneous:
 ( ): indicate index of already defined matrix
 [ ]: defines the values of a new matrix
Common error messages
>>A = [1 2 3] - [2 0];
>>A = [1 2 3] * [1 2 3];
>>A = [1 2 3]^2;
>> A = [1,3];
>> A(3)
>> A(-1)
???Error using ==> minus
Matrix dimensions must agree.
??? Error using ==> mtimes
Inner matrix dimensions must agree.
??? Error using ==> mpower
Matrix must be square.
??? Index exceeds matrix dimensions.
??? Subscript indices must either be real positive integers or logicals.
>> A(i)
>> A(1.5)
>> A(0)
>> A(1::, 2)
??? A(1::, 2)
|
Error: Unexpected MATLAB operator.
4) Datatype: Boolean
Note: can be thought of as a logic (TRUE of FALSE) when you compare “expressions”
 Compare: ==, ~=, >, >=, <, <=
 Logical: &, |, ~
>>if a = 3
>> A(1
>>A(1))
>>A = 1+3+
>>A = sin()
??? if a = 3
|
Error: The expression to the left of the equals sign is not a valid target
for an assignment.
??? A(1
|
Error: Expression or statement is incorrect--possibly unbalanced (, {,
or [.
??? A(1))
|
Error: Unbalanced or misused parentheses or brackets.
??? A = 1+3+
|
Error: Expression or statement is incomplete or incorrect.
??? A = sin()
|
Error: Expected a variable, function, or constant, found ")".
5) Others
 What if you have a really long expression, you can write a single expression in multiple lines by using …
 Converting a number into string so that you may put it in a sentence: num2str()
>> num = 2008; disp([‘I will graduate in the year ’ num2str(num)];
 What if you want the ask the user for input:
>> Response = input(‘How old are you?’);
>> if Response < 0,
>>
disp(‘please enter a positive number’);
>> end;
6) Introduction to function (We shall talk more about functions next week)
A function is a collection of codes / scripts that enables us to perform the same operations many times given the
same set of rules but with different parameters. A “sin” operator is a build-in function of Matlab.
Example, generate a file called “computeGPA.m” with the following script
[grade]=computeGPA(marks),
if marks >= 90, grade = ‘A’;
elseif marks < 90 & marks >=80, grade = ‘B’;
elseif marks < 80 & marks >=70, grade = ‘C’;
elseif marks < 70 & marks >=60, grade = ‘D’;
else, grade = ‘F’;
end;
Download