DATA(Variables) Types
Character
1)A character is represented in ASCII using a numeric code between 0 to 255(字符通过ASCII用0到255之间的数字代码表示)
2)Create a character or a string by putting them into a pair of apostrophe(将一个字符或字符串放入一对引号中)
示例:
clc
clear
s1 = 'h'
whos
uint16(s1)
String
1)An array collects characters:(通过数组收集字符)
2)String concatenation:(字符串的连接)
示例:
s1 = 'Example';
s2 = 'String';
s3 = [s1,s2];
s4 = [s1;s1];
fprintf('s1 = %s \n s2 = %s \n s3 = %s \n s4 = %s',s1,s2,s3,s4);
输出结果:
s1 = Example
s2 = String
s3 = ExampleString
s4 = Example
Example
注意:·s4 = [s1;s1] 在使用时需要长度相等,结果是叠加
Logical Operations and Assignments(逻辑操作和分配)
1)Many numerical and logical operators can be applied to strings(许多数字和逻辑运算符可以应用于字符串)
示例:
str = 'aardvark';
'a' == str
输出结果:(查找str字符串中,若等于‘a’则为1,否则为0)
2)Try this
str = 'aardvark';
'a' == str
str(str == 'a') = 'z'
输出结果:(将str字符串中为‘a’的替换成字母‘z’)
3)What if we want to compare the entries string with another?(如果我们想将整个字符串与另一个字符串进行比较呢)
使用:tf=strcmp(s1,s2)
tf=strcmp(s1,s2)比较s1和s2,如果两者相同,则返回1(true),否则返回0(false)。
Structure(结构体)
1)A method of storing heterogeneous data(一种存储异构数据的方法)
2)Structures contain arrays called fields(结构包含称为字段的数组)
3)Student assignment grade:(例如学生作业成绩)
示例:
student.name = 'John Doe';
student.id = 'jdo2@sfu.ca';
student.number = 301073268;
student.grade = [100,75,73;...
95,91,85.5;...
100,98,72];
disp(student)
disp(student.grade)