字符串指令
- 创建字符串
- 使用单引号将字符序列括起来创建字符串
- 使用单引号创建的字符串是一个字符数组,每个字符都被视为一个独立的元素
- 可以通过索引访问每个字符
- 使用双引号创建的字符串是一个字符串数组,整个字符串被视为一个元素
- 无法通过索引访问单个字符
>> a = "Hello World!"a = "Hello World!">> b = 'Hello World!'b ='Hello World!'>> b(1)ans ='H'
- 拼接字符串
- strcat
- 水平地连接成单个字符串
- strcat
>> str = strcat('Hello', 'World!')str ='HelloWorld!'
-
- strvcat
>> str = strvcat('Hello', 'Wrold!')str =2×6 char 数组'Hello ''Wrold!'
-
- char
>> str = char('Hello', 'World!')str =2×6 char 数组'Hello ''World!'
- 字符串长度
>> str = 'Hello World!'str ='Hello World!'>> length(str)ans =12
- 子字符串提取
>> str(2:7)ans ='ello W'
- 字符串比较
str1 = "Hello">> str2 = "Hello"str2 = "Hello">> str3 = "hello"str3 = "hello">> strcmp(str1, str2)ans =logical1>> strcmp(str1, str3)ans =logical0
- 字符串查找
>> str = 'Hello James, Hello World!'str ='Hello James, Hello World!'>> str2 = 'James'str2 ='James'>> strfind(str, str2)ans =7
- 字符串替换
str ='Hello James, Hello World!'>> str2 = 'James'str2 ='James'>> strfind(str, str2)ans =7>> str3 = 'Potter'str3 ='Potter'>> strrep(str, str2, str3)ans ='Hello Potter, Hello World!'
- 字符串转换
- str2num函数
- 将字符数组或字符串转换为数值数组
- str2num函数
>> str = '123456789'str ='123456789'>> num1 = str2num(str)num1 =123456789>> num1(1)ans =123456789
-
- num2str函数
- 将数值转换为字符串
- num2str函数
>> num = 123456789num =123456789>> str = num2str(num)str ='123456789'>> str(1)ans ='1'
Matlab绘图指令
- 一维绘图
plot(x)
- 二维绘图
plot(x, y)
- 多次二维绘图
plot(x1, y1, x2, y2)
>> x1 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
>> y1 = [2, 3, 1, 4, 7, 3, 2, 1, 3];
>> x2 = [3, 4, 5, 6, 7, 8, 9, 10, 11 ];
>> y2 = [9, 3, 2, 3, 5, 6, 8, 2, 1];
- 对数图绘制
semilogx(x1, y1)
semilogy(x1, y1)
loglog(x1, y1)
- 极坐标
>> theta = linspace(0, 2*pi, 100);
>> rho = cos(theta).^2;
>> polar(theta, rho)
- 直方图
>> randnum = rand(1000);
>> hist(randnum)