前言
此篇文章是我在B站学习时所做的笔记,语句部分为亲自动手演示过的,方便复习用。此篇文章仅供学习参考。
提示:以下是本篇文章正文内容,下面案例可供参考
一、变量(数据)类型转换
double() | 转换为双精度 |
---|---|
single() | 转换为单精度 |
int8() | 转换为8位有符号整数 |
int16() | 转换为16位有符号整数 |
int32() | 转换为32位有符号整数 |
int64() | 转换为64位有符号整数 |
uint8() | 转换为8位无符号整数 |
uint16() | 转换为16位无符号整数 |
uint32() | 转换为32位无符号整数 |
uint64() | 转换为64位无符号整数 |
1、char字符
- 一个字符在ASCll中使用0到255之间的数字代码表示
- 创建一个字符或字符串,将它们放入一对撇号中:
>> s1 = 'h'
whos
uint16(s1)
s1 =
'h'
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 1 int8
s1 1x1 2 char
ans =
uint16
104
>> s2 = 'H'
whos
uint16(s2)
s2 =
'H'
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 1 int8
ans 1x1 2 uint16
s1 1x1 2 char
s2 1x1 2 char
ans =
uint16
72
这个ASCII码图表是转载会飞的鱼博客的
2、String
数组收集字符:
字符串连接:
注意:此处的s4=[s1;s2];会出现错误,因为数组的维度不一样,即长度不一样,所以只能连接长度相同的数组。
>> s1='Example';
>> s2='String';
>> s3=[s1 s2]
s3 =
'ExampleString'
>> s4=[s1;s2]
错误使用 vertcat
要串联的数组的维度不一致。
>> s4=[s1;s1]
s4 =
2×7 char 数组
'Example'
'Example'
拓展:
>> A1 = ["str1" "str2"];
A2 = ["str3" "str4"];
A3 = ["str5" "str6"];
C = vertcat(A1,A2,A3)
C =
3×2 string 数组
"str1" "str2"
"str3" "str4"
"str5" "str6"
%等价于---------------------------------------
>> c=[A1;A2;A3]
c =
3×2 string 数组
"str1" "str2"
"str3" "str4"
"str5" "str6"
3、逻辑操作与赋值
许多数字和逻辑运算符可以应用于字符串
>> str = 'aardvark';
>> str(3)
ans =
'r'
>> 'a' == str
ans =
1×8 logical 数组
1 1 0 0 0 1 0 0
>> str=='a'
ans =
1×8 logical 数组
1 1 0 0 0 1 0 0
>> str(str == 'a') = 'Z'
str =
'ZZrdvZrk'
如果我们想要将整个字符串与另一个字符串进行比较,可以用strcmp函数
>> help strcmp
strcmp - 比较字符串
此 MATLAB 函数 比较 s1 和 s2,如果二者相同,则返回 1 (true),否则返回 0
(false)。如果文本的大小和内容相同,则它们将视为相等。返回结果 tf 的数据类型为 logical。
tf = strcmp(s1,s2)
另请参阅 eq, ne, regexp, regexpi, sort, strcmpi, strfind, strncmp, strncmpi
strcmp 的文档
名为 strcmp 的其他函数
>> str1='arrdvark'
str1 =
'arrdvark'
>> strcmp(str,str1)
ans =
logical
0
>> str
str =
'ZZrdvZrk'
Exercise练习
编写一个脚本来反转任何给定的字符串
>> s1='I like the letter E'
s1 =
'I like the letter E'
-------------------------------------------------------
>> s2 = s1(size(s1,2):-1:1)
s2 =
'E rettel eht ekil I'
-------------------------------------------------------
>> s2 = s1(length(s1):-1:1)
s2 =
'E rettel eht ekil I'
-------------------------------------------------------
>> help reverse
reverse - 反转字符串中的字符顺序
此 MATLAB 函数 反转 str 中字符的顺序。
newStr = reverse(str)
另请参阅 eq, flip, fliplr, lower, replace, reshape, upper
reverse 的文档
名为 reverse 的其他函数
>> reverse(s1)
ans =
'E rettel eht ekil I'
-----------------------------------------------------
>> help flip
flip - 翻转元素顺序
此 MATLAB 函数 返回的数组 B 具有与 A 相同的大小,但元素顺序已反转。B 中重新排序的维度取决于 A 的形状:
B = flip(A)
B = flip(A,dim)
另请参阅 fliplr, flipud, permute, rot90, transpose
flip 的文档
名为 flip 的其他函数
>> flip(s1)
ans =
'E rettel eht ekil I'
二、Struct结构体数组
- 一种存储异构数据的方法
- 结构包含称为字段的数组
1、创建一个结构
学生作业的成绩:
>> student.name = 'John Doe';
student.id = 'jdo2@sfu.ca';
student.number = 301073268;
student.grade = [100, 75, 73; ...
95, 91, 85.5; ...
100, 98, 72];
>> student
student =
包含以下字段的 struct:
name: 'John Doe'
id: 'jdo2@sfu.ca'
number: 301073268
grade: [3×3 double]
>> student.grade
ans =
100.0000 75.0000 73.0000
95.0000 91.0000 85.5000
100.0000 98.0000 72.0000
2、向结构中添加信息
>> student(1).name = 'John Doe';
student(1).id = 'jdo2@sfu.ca';
student(1).number = 301073268;
student(1).grade = [100, 75, 73; ...
95, 91, 85.5; ...
100, 98, 72];
>> student(2).name = 'Ann Lane';
student(2).id = 'aln4@sfu.ca';
student(2).number = 301078853;
student(2).grade = [95 100 90; 95 82 97; 100 85 100];
>> student
student =
包含以下字段的 1×2 struct 数组:
name
id
number
grade
3、显示结构中某个数值
如果要显示第二个学生的某个成绩,如显示90分,则:
>> student(2).grade(7)
ans =
90
>> student(2).grade(1,3)
ans =
90
4、结构体功能
cell2struct | 将单元格数组转换为结构数组 |
---|---|
fieldnames | 结构的字段名,或对象的公共字段 |
getfield | 结构体数组字段 |
isfield | 确定输入是否为结构体数组字段 |
isstruct | 确定输入是否为结构数组 |
orderfield | 结构数组的顺序字段 |
rmfield | 从结构中删除字段 |
setfield | 给结构数组字段赋值 |
struct | 创建结构数组 |
struct2cell | 将结构转换为单元格数组 |
structfun | 对标量结构的每个域应用函数 |
rmfield 函数举例:
>> fieldnames(student)
ans =
4×1 cell 数组
{'name' }
{'id' }
{'number'}
{'grade' }
>> student
student =
包含以下字段的 1×2 struct 数组:
name
id
number
grade
>> rmfield(student,'id')
ans =
包含以下字段的 1×2 struct 数组:
name
number
grade
三、嵌套结构
>> A = struct('data', [3 4 7; 8 0 1], 'nest', ...
struct('testnum','Test 1', ...
'xdata', [4 2 8],'ydata', [7 1 6]));
A(2).data = [9 3 2; 7 6 5];
A(2).nest.testnum = 'Test 2';
A(2).nest.xdata = [3 4 2];
A(2).nest.ydata = [5 0 9];
>> A.nest
ans =
包含以下字段的 struct:
testnum: 'Test 1'
xdata: [4 2 8]
ydata: [7 1 6]
ans =
包含以下字段的 struct:
testnum: 'Test 2'
xdata: [3 4 2]
ydata: [5 0 9]
>> A(1)
ans =
包含以下字段的 struct:
data: [2×3 double]
nest: [1×1 struct]
>> A(1).data
ans =
3 4 7
8 0 1
>> A(1).nest
ans =
包含以下字段的 struct:
testnum: 'Test 1'
xdata: [4 2 8]
ydata: [7 1 6]
四、Cell元胞数组
- 存储异构数据的另一种方法
- 类似于矩阵,但每个条目包含不同类型的数据
- 通过将索引括在圆括号 () 中可以引用元胞集。使用花括号 {} 进行索引来访问元胞的内容。
对于cell不懂的地方,可以看看这个同学的文章:matlab的cell数组
1、创建对象和访问
创建对象
当要将数据放入一个元胞数组中时,请使用元胞数组构造运算符 {} 创建该数组。
方法一
>> A(1,1)={[1 4 3; 0 5 8; 7 2 9]};
A(1,2)={'Anne Smith'};
A(2,1)={3+7i};
A(2,2)={-pi:pi:pi};
>> A
A =
2×2 cell 数组
{3×3 double } {'Anne Smith'}
{[3.0000 + 7.0000i]} {1×3 double }
方法二
>> A{1,1}=[1 4 3; 0 5 8; 7 2 9];
A{1,2}='Anne Smith';
A{2,1}=3+7i;
A{2,2}=-pi:pi:pi;
>> A
A =
2×2 cell 数组
{3×3 double } {'Anne Smith'}
{[3.0000 + 7.0000i]} {1×3 double }
您也可以使用 {} 创建一个空的 0×0 元胞数组。
>> B={}
B =
空的 0×0 cell 数组
原理
- 单元格数组中的每个条目都持有一个指向数据结构的指针
- 同一单元阵列的不同单元可以指向不同类型的数据结构
访问单元阵列
>> A(1,1)={[1 4 3; 0 5 8; 7 2 9]};
A(1,2)={'Anne Smith'};
A(2,1)={3+7i};
A(2,2)={-pi:pi:pi};
>> A
A =
2×2 cell 数组
{3×3 double } {'Anne Smith'}
{[3.0000 + 7.0000i]} {1×3 double }
>> C=A{1,1}
C =
1 4 3
0 5 8
7 2 9
>> D=A(1,1)
D =
1×1 cell 数组
{3×3 double}
>> C=A{1,1}(6)
C =
2
>> C=A{1,1}(3,2)
C =
2
2、Cell元胞数组函数
cell | 创建单元阵列 |
---|---|
cell2mat | 将元胞数组转换为基础数据类型的普通数组 |
cell2struct | 将元胞数组转换为结构体数组 |
celldisp | 显示元胞数组的内容 |
cellfun | 对元胞数组中的每个元胞应用函数 |
cellplot | 以图形方式显示元胞数组的结构体 |
cellstr | 转换为字符向量元胞数组 |
iscell | 确定输入是否为元胞数组 |
mat2cell | 将数组转换为在元胞中包含子数组的元胞数组 |
num2cell | 将数组转换为相同大小的元胞数组 |
struct2cell | 将结构体转换为元胞数组 |
num2cell()和matcell()
其中,mat2cell(a,[1 1 1],3)中,a是数组,[1 1 1]是行,3是列
>> a = magic(3)
a =
8 1 6
3 5 7
4 9 2
>> b = num2cell(a)
b =
3×3 cell 数组
{[8]} {[1]} {[6]}
{[3]} {[5]} {[7]}
{[4]} {[9]} {[2]}
>> c = mat2cell(a, [1 1 1], 3)
c =
3×1 cell 数组
{1×3 double}
{1×3 double}
{1×3 double}
五、多维数组
cat()
数组连接
reshape()
返回具有已分配的行和列的新数组
>> A = {'James Bond', [1 2;3 4;5 6]; pi, magic(5)}
C = reshape(A,1,4)
A =
2×2 cell 数组
{'James Bond'} {3×2 double}
{[ 3.1416]} {5×5 double}
C =
1×4 cell 数组
列 1 至 3
{'James Bond'} {[3.1416]} {3×2 double}
列 4
{5×5 double}
使用重塑从下面的矩阵a创建一个矩阵B:
A =
1 2 3
4 5 6
>> B=reshape(A,3,2)
B =
1 5
4 3
2 6
检查变量和变量状态
isinteger | 确定输入是否为整型数组 |
---|---|
islogical | 判断输入是否为逻辑阵列 |
isnan | 检测非数字元素(NaN) |
isnumeric | 确定输入是否为数字数组 |
isprime | 检测数组的质数元素 |
isreal | 确定所有数组元素是否都是实数 |
iscell | 确定输入是否为元胞数组 |
ischar | 确定输入是否为字符数组 |
isempty | 确定输入是否为空数组 |
isequal | 确定数组在数值上是否相等 |
isfloat | 确定输入是否是浮点数组 |
isglobal | 确定输入是否是全局变量 |
ishandle | 检测有效的图形对象句柄 |
isinf | 检测数组的无限元素 |
六、文件访问
save()和load()
-
保存(所有)工作空间数据到一个文件:
-
加载存储在文件中的数据:
注意
- 不加‘-ascii’后,生成的mat文件打开会出现乱码。
- 在save文件时有-ascii时,load文件时也应该要有。
Excel文件读取:xlsread ()
读取Excel电子表格
注意:所要读取的Excel表格应该和运行文件档案同个文件夹,比如:此处运行是在E:matlab例子里边运行,所以要把Excel放在同个文件夹才能读取成功。
Excel文件写入:xlswrite ()
计算平均值并写入Excel电子表格
在Excel电子表格中获取文本
低级文件输入/输出
- 在字节或字符级别读取和写入文件
- 文件ID为fid
- 文件中的位置由可移动的指针指定
低级文件I/O功能
函数 | 描述 |
---|---|
fopen | 打开文件,或获取打开文件的信息 |
fclose | 关闭一个或所有打开的文件 |
fscanf | 从文本文件读取数据 |
fprintf | 将数据写入文本文件 |
feof | 测试文件尾 |
打开和关闭文件:
将正弦值写入文件
通过格式化I/O进行读写
读取文件
如若侵权,请及时与我联系。
此篇文章仅供学习参考,禁止转载!