我所有的学习笔记都是基于Matlab2012b的Help文档,因为是英文,所以自己可能理解的有问题,如发现错误,还望指正。
1.Enter Statements in Command Window
我们可以在命令窗口下输入独立的语句(individual statements),例如创建一个变量a,可以这样输入:
a=1
这样之后就会把a加入到workspace中,并且在命令窗口中显示:
a =
1
如果以分号(semicolon)结尾,那么就不会显示结果,例如:
b = 2;
为了输入多条语句,但是并不想每输入一条语句就执行,可以使用Shift+Enter在每输入一条语句之后。
可以在一行中输入多个语句,但是各语句之间应该用逗号(comma)或者分号(semicolon)隔开。例如:
A = magic(5), B = ones(5) * 4.7; C = A./B
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
C =
3.6170 5.1064 0.2128 1.7021 3.1915
4.8936 1.0638 1.4894 2.9787 3.4043
0.8511 1.2766 2.7660 4.2553 4.6809
2.1277 2.5532 4.0426 4.4681 0.6383
2.3404 3.8298 5.3191 0.4255 1.9149 这里请注意,因为B语句是以分号结尾的,所以不会显示B的结果。
可以使用↑和↓来调用之前输入的语句,还可以输入开头的几个字母,然后用↑和↓可以更快的确定语句,例如,为了重新写b=2,这是输入b,然后按↑或↓。
2.Format Output in Command Window
(1)Format Line Spacing in Output
有两种格式:loose and compact
loose:保持输出之间有空行
>> x = [4/3 1.2345e-6]
x =
1.3333 0.0000 compact:各条语句之间不存在空行
>> x = [4/3 1.2345e-6]
x =
1.3333 0.0000 分别使用如下语句来设置:
format loose
format compact (2)Format Floating-Point Numbers
Matlab默认使用short format:5-digit scaled,定点表示。自己可以改变。例如:
>> format short
>> 123
ans =
123
>> 12.3
ans =
12.3000
>> format short e
>> 123
ans =
123
>> 123456
ans =
123456
>> 12.3456
ans =
1.2346e+01
自己可以多试试。
3.Call Functions
我们先创建两个数值数组:
A = [1 3 5];
B = [10 6 4];
输入参数要用括号(parentheses)括起来:
max(A)
多个输入参数之间要用逗号(comma)隔开:
max(A,B)
可以把输出参数赋值给一个变量:
maxA = max(A)
多个输出参数用方括号(square brackets)
[maxA, location] = max(A)
对于那些既没有输入参数也没有输出参数的函数,调用的时候直接写函数名就可以了:
clc
对于输入参数是字符文本的应用单引号(single quotation marks)括起来:
disp('hello world')
4.Continue Long Statements on Multiple Lines
我们使用ellipses(...)来实现:
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 ...
- 1/6 + 1/7 - 1/8 + 1/9;
把两个短的字符串连接成长的字符串:
mystring = ['Accelerating the pace of ' ...
'engineering and science'];
每一行都必须出现单引号(single quotation marks),否则就会返回错误。如下:
mystring = 'Accelerating the pace of ...
engineering and science'
除了在字符串中,在其他地方就相当于空格(space)。 例如:
x = [1.23...
4.56];
等价于
x = [1.23 4.56];
5.Stop Execution
为了停止运行可以使用Ctrl+C或者Ctrl+Break。
Ctrl+C可能会存在一些问题,例如当程序出现无限循环的时候可能并不能退出。使用时注意一点。