1. MATLAB 数据(变量)
1.1 类型
1.2 变量(数据)类型的转换
% 函数列表:
double( )
single( )
int8( ) int16( ) int32( ) int64( )
uint8( ) uint16( ) uint32( ) uint64( )
EX:
>> A=20
A =
20
>> B=int8(A)
B =
20
>> whos
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 1 int8
ans 1x3 24 double
1.3 字符或字符串 < char or string>
1.3.1 字符
- 字符在 ASCII 中用 0 到 255 之间的数字代码表示
>> s1 = 'h'
whos
uint16(s1)
s1 =
h
Name Size Bytes Class Attributes
s1 1x1 2 char
ans =
104
- ASCII对照表:
1.3.2 字符串
>> s1 = 'Example';
s2 = 'String';
>> s3 = [s1 s2]
s3 =
ExampleString
>> s4 = [s1; s2]
错误使用 vertcat % 此处s1与s2字母数量不一致,故报错
串联的矩阵的维度不一致。
- 逻辑运算和赋值
>> str = 'aardvark';
'a' == str
ans =
1 1 0 0 0 1 0 0
>> str(str == 'a') = 'Z'
str =
ZZrdvZrk
1.3.3 练习
-
编写一个反转任何给定字符串的脚本
s1 = ‘I like the letter E’
s2 = ‘E rettel eht ekil I’
1.4 Structure
- 一种存储异构数据的方法;
- 结构包含称为字段的数组;
1.4.1 EX:
>> student.name = 'John Doe';
student.id = '[email protected]';
student.number = 301073268;
student.grade = [100, 75, 73; ...
95, 91, 85.5; ...
100, 98, 72];
student
student =
name: 'John Doe'
id: '[email protected]'
number: 301073268
grade: [3x3 double]
- 向结构添加信息
student(2).name = 'Ann Lane';
student(2).id = '[email protected]';
student(2).number = 301078853;
student(2).grade = [95 100 90; 95 82 97; 100 85 100];