Matlab tutorials:
Exercise 1.1: Entering commands
You are supposed to perform the following exercises in the specified order. Before entering each of the commands, think about the output on the screen and the result of each exercise.
- a)
>> a = 1+2+3+4+5+6
- b)
>> b = 1+2+3+4+5+6;
- c)
>> b+3
- d)
>> 1+2+3+4+5+6
- e)
>> c = 10*ans
Exercise 1.2: Interruption of calculations or output
Enter the array 0:900000 and make sure the output appears in the MATLAB Command Window.
Perform the exercise again and and use the key combination `ctrl-c' quickly. What is the effect? -
Exercise 1.3: Incomplete commands
By entering
the array
is specified to the variable
.
- a)
- Enter
>> a = [2,3,4
and use the `return' key. Finish the exercise.
- b)
- Enter
>> a = [2,3,4
and use the `return' key. Interrupt the input of the exercise. -
-
Exercise 1.4: Removal of variables
Define the variables
and
and then delete them. Check if the variables are actually deleted. -
Exercise 1.5: Saving data files
Perform the following two exercises: >> f = 2
and >> g = 3 + f
Save the variables
and
in the file `try.mat'.
Close MATLAB and start it again. Load the file `try.mat' and see if the variables
and
are known. Where is the file `try.mat' placed?
Delete the file `try.mat'. -
-
- Have you tried all these exercise yet, if not try now. I think you have got some answers with you do not you? Ok, now this time to check the answer. Did you get the following answer? Please compare and comment if it is not same:
-
Answer 1.1
- a)
>> a = 21
The result of the sum
is ascribed to the variable
. De output
appears in the MATLAB Command Window.
- b)
>> ()
The result of the sum
is ascribed to the variable
. The output is not visible in the MATLAB Command Window because the semicolon sign (;) is used. However, it is possible to call up the variable
.
- c)
>> ans = 24
The output of the sum
appears in the Matlab Command Window as
. No variable is ascribed to this calculation. In such a case the result is automatically ascribed to the variable
.
- d)
>> ans = 21
Also in this exercise no variable is ascribed to the calculation. The output is thus as follows:
.
- e)
>> c = 210
The result of this exercise depends on the previous answer because the variable
is used in this calculation. Assume that
(obtained from exercise d), then the answer on this question becomes:
. Answer 1.2
By using the key combination `ctrl-c' you can interrupt a calculation or output in MATLAB. The effect of the key combination `ctrl-c' in this exercise is that the output is early canceled, considering the fact that you apply the key combination in time.
OUTPUT SUMMARY:
>> 0:90000
key combination: ctrl-c Answer 1.3
- a)
>> a = [2 3 4
The exercise
can be finished, after entering the `return' key, by closing the array yet with a bracket ] and again enter the `Return' key. The array [2 3 4] is now ascribed to the variable
.
- b)
>> ctrl-c ()
The exercise
can be interrupted by using the command `ctrl-c'. The calculation is then stopped and nothing is ascribed to the variable
. -
Answer 1.4
>> u = 2:4; v = [1 5];
>> who
>> clear u v
>> who
Answer 1.5
Existing variables can be saved in a mat-file by using the command >> save filename.mat
. The file is written to the current active directory (see: `Current directory' in the MATLAB taskbar). Later on it is possible to call up the variables by loading the mat-file (pay attention that you are working in the directory in which MATLAB has written the mat-file) by using the command >> load filename
. Entering the command >> who
gives a list with used variables. Entering the command >> what
gives a list with existing mat-files in that current directory. The mat-file can be deleted by entering the command >> delete filename.mat
.
INPUT SUMMARY: >> f = 2
>> g = 3 + f
>> save try.mat f g
% Exit Matlab and start it again
>> who
>> what
>> delete try.mat