1.整数型
>> a = 240;
>> b1 = int8(a)
b1 =
127
>> b2 = int16(a)
b2 =
240
>> b3 = uint8(a)
b3 =
240
>> b4= uint16(a)
b4 =
240
2.浮点型
>> a = 123.567
a =
123.5670
>> b = single(a)
b =
123.5670
>> c = int16(a)
c =
124
>> whose
Undefined function or variable 'whose'.
Did you mean:
>> whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x1 4 single
取整
>> a1 = round(-1.9) %就近取整
a1 =
-2
>> a2 = round(3.4)
a2 =
3
>> b1 = fix(-1.9) %舍去小数部分取整
b1 =
-1
>> b2 = fix(3.4)
b2 =
3
>> c1 = floor(-1.9) %结果小于浮点数就近取整
c1 =
-2
>> c2 = floor(3.4)
c2 =
3
>> d1 = ceil(-1.9) %结果大于浮点数就近取整
d1 =
-1
>> d2 = ceil(3.4)
d2 =
4
>>
字符串的基本操作
>> S = 'Please create a string' %创建字符串
S =
Please create a string!
>> [m, n] = size(S) %计算字符串的大小
m =
1
n =
23
>> S1 = double(S) %计算字符串的ASCII码
S1 =
Columns 1 through 9
80 108 101 97 115 101 32 99 114
Columns 10 through 18
101 97 116 101 32 97 32 115 116
Columns 19 through 23
114 105 110 103 33
>> S = lower(S) %将所有字母转成小写字母
S =
please create a string!
>> S2 = upper(S) %将所有字母转成大写字母
S2 =
PLEASE CREATE A STRING!
>>
字符串的连接和拆分
>> s1 = 'How are you!'
s1 =
How are you!
>> s2 = 'Fine, Thank you!'
s2 =
Fine, Thank you!
>> a = [s1, s2] %合并字符数组
a =
How are you!Fine, Thank you!
>> b = char(s1, s2) %连接字符串
b =
How are you!
Fine, Thank you!
>> c = strcat(s1, s2) %横向连接字符串
c =
How are you!Fine, Thank you!
>> d = strvcat(s1, s2) %纵向连接字符串
d =
How are you!
Fine, Thank you!
>> e = s2(7:16)
e =
Thank you!
字符串的比较
>> s1 = 'My name is Tommy'
s1 =
My name is Tommy
>> s2 = 'Nice to meet you'
s2 =
Nice to meet you
>> a = s1 == s2
a =
Columns 1 through 9
0 0 0 0 0 0 0 1 0
Columns 10 through 16
0 0 0 0 0 0 0
>> b = s1>s2
b =
Columns 1 through 9
0 1 0 1 1 0 0 0 0
Columns 10 through 16
1 0 0 1 0 0 1
>> c = lt(s1, s2)
c =
Columns 1 through 9
1 0 1 0 0 1 1 0 1
Columns 10 through 16
0 1 1 0 1 1 0
>> d = s1<s2
d =
Columns 1 through 9
1 0 1 0 0 1 1 0 1
Columns 10 through 16
0 1 1 0 1 1 0
>>
字符与数值之间的转换
>> num = rand(3, 3)
num =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> s1 = num2str(num)
s1 =
0.81472 0.91338 0.2785
0.90579 0.63236 0.54688
0.12699 0.09754 0.95751
>> s2 = num2str(num, 10)
s2 =
0.8147236864 0.9133758561 0.2784982189
0.9057919371 0.6323592462 0.5468815192
0.1269868163 0.097540405 0.9575068354
>> s2 = num2str(pi, 10)
s2 =
3.141592654
>> int = 12345
int =
12345
>> s3 = int2str(int)
s3 =
12345
>> s4 = mat2str(pascal(3))
s4 =
[1 1 1;1 2 3;1 3 6]
>> pascla(3)
Undefined function or variable 'pascla'.
Did you mean:
>> pascal(3)
ans =
1 1 1
1 2 3
1 3 6
>> num1 = str2num('123456')
num1 =
123456
>> num2 = str2double('12345.6')
num2 =
1.2346e+04
>>