MATLAB基础知识学习笔记
03 变量与数据存取
3.1 MATLAB数据(变量)
3.1.1 类型
3.1.2 数据类型转换
% 函数列表:
double( )
single( )
int8( ) int16( ) int32( ) int64( )
uint8( ) uint16( ) uint32( ) uint64( )
举例
3.1.3 字符与字符串
Char
- 字符在 ASCII 中用 0 到 255 之间的数值表示
- 通过将字符或字符串放入
' '
中来创建字符或字符串
举例
ASCII码对照表
String
s4 = [s1; s2]
报错原因:此处s1与s2字母数量不一致,若果两个字符串长度一致,则不会报错,完成拼接。
逻辑运算和赋值
这里将
'a'
与str
的每一个位置做逻辑运算,返回大小与str一致的01矩阵,其中1代表相等,0代表不相等
将str中的'a'
替换为'Z'
注:如果想要比较两个字符串是否相等可以使用
strcmp(str1, str2)
函数进行判断
练习
编写一个反转任何给定字符串的脚本
function FZ()
s1 = 'I like the letter E';
s2 = [];
for i=1:size(s1,2)
s2(size(s1,2)-i+1) = s1(i);
end
disp(char(s1));
disp(char(s2));
也可以直接使用reverse()
方法
s1 = 'I like the letter E';
s2 = reverse(s1);
disp(s1);
disp(s2);
3.1.4 Structure(结构体)
- 一种存储异构数据的方法;
- 结构包含称为字段的数组;
举例
student.name = 'Stefan';
student.id = 'jdfffooo.cc';
student.number = 3929844;
student.grade = [100,75,73;...
95,91,85.5;...
100,98,72];
student
向结构体里添加信息
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(2).grade(1,3)
与结构体相关的函数
函数名 | 说明 |
---|---|
cell2struct() | 将元胞数组转换为结构体数组 |
fieldnames() | 返回元胞数组中结构体数组的各个字段名称 |
getfield() | 返回结构体中指定字段的值 |
isfield() | 判断输入的字段是否是结构体里的 |
isstruct() | 判断输入的是否是结构体 |
orderfields() | 给指定结构体中的字段按名称(ASCII码)排序 |
rmfield() | 删除结构体中指定的字段 |
struct() | 创建结构体数组放到变量中 |
struct2cell() | 将结构体数组转换为元胞数组 |
structfun() | 对标量结构的每个域应用函数,返回任何数据类型的数组 |
嵌套结构
3.1.5 元胞数组(Cell Array)
什么是元胞数组?
-
元胞数组与矩阵类似,但每个条目可以包含不同类型的数据;
[ [ 1 4 3 0 5 8 7 2 9 ] ′ A n n e S m i t h ′ 3 + 7 i [ − π 0 π ] ] \begin{bmatrix} \begin{bmatrix} 1 & 4 & 3\\ 0 & 5 & 8\\ 7 & 2 & 9 \end{bmatrix} & 'Anne Smith'\\ 3+7i & [-π 0 π] \end{bmatrix} ⎣⎢⎢⎡⎣⎡107452389⎦⎤3+7i′AnneSmith′[−π0π]⎦⎥⎥⎤ -
使用
{}
来声明;
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{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
如何读取元胞数组?
大括号{}
用于访问元胞数组的“内容”
元胞数组相关函数
函数名 | 说明 |
---|---|
cell() | 创建元胞数组,用 { } |
cell2mat() | 将元胞数组转换为基础数据类型的普通数组 |
celldisp() | 显示元胞数组内容 |
cellfun() | 对元胞数组中每个元胞应用函数,各自返回对应函数的结果 |
cellplot() | 以图形方式显示元胞数组的结构体 |
cellstr() | 转换为字符向量元胞数组 |
iscell() | 判断输入是否是元胞数组 |
mat2cell() | 将数组转换为在元胞中包含子数组的元胞数组 |
num2cell() | 将数组转换为相同大小的元胞数组 |
3.1.6 多维数组(Multi-dimensional Array)
方法一
A{1,1,1} = [1 2;4 5];
A{1,2,1} = 'Name';
A{2,1,1} = 2-4i;
A{2,1,1} = 7;
A{1,1,2} = 'Name2';
A{1,2,2} = 3;
A{2,1,2} = 0:1:3;
A{2,2,2} = [4 5]';
方法二:cat()
串联数组
A{1,1} = [1 2;4 5];
A{1,2} = 'Name';
A{2,1} = 2-4i;
A{2,2} = 7;
B{1,1} = 'Name2';
B{1,2} = 3;
B{2,1} = 0:1:3;
B{2,2} = [4 5]';
C = cat(3, A, B)
reshape()
重组矩阵形状
函数说明:将矩阵形状进行重组
语法:
reshape(array, row, col)
前提条件:row * col = array的元素数
A = {'James Bond', [1 2;3 4;5 6]; pi, magic(5)}
C = reshape(A,1,4)
3.1.7 检查变量和变量状态
函数 | 功能 |
---|---|
isinteger() | 确定输入是否为整数数组 |
islogical() | 确定输入是否为逻辑数组 |
isnan() | 检测不是数字(NaN)的元素 |
isnumeric() | 确定输入是否为数字数组 |
isprime() | 检测数组的素元素 |
isreal() | 确定所有数组元素是否都是实数 |
iscell() | 确定输入是否为原胞数组 |
ischar() | 确定输入是否为字符数组 |
isempty() | 确定输入是否为空数组 |
isequal() | 确定数组在数值上是否相等 |
isfloat() | 确定输入是否为浮点数组 |
isglobal() | 确定输入是否为全局变量 |
isinf() | 检测无限数组元素 |
3.2 数据存取
3.2.1 文件存取
save()
和load()
-
保存workspace中所有的数据到
.mat
文件中;clear; a = magic(4); save mydata1.mat % 打开会乱码; save mydata2.mat -ascii % 使mydata1.mat文件在其他程序中打开不乱码;
-
加载存储在文件中的数据;
load('mydata1.mat') load('mydata2.mat','-ascii') % 使用ascii解码;
3.2.2 Excel文件存取
xlsread()
score = xlsread('04score.xlsx')
score = xlsread('04score.xlsx', 'B2:D4') % 读取注明单元格的内容;
12
xlswrite()
M = mean(score')'; % mean()函数是column 运算,而score中每个学生的成绩是row,故先转置 ' ,求得平均数,在转置回来;
xlswrite('04score.xlsx', M, 1, 'E2:E4'); % xlswrite('filename', 'variable', 'sheet(页)', 'location');
xlswrite('04score.xlsx', {'Mean'}, 1, 'E1'); % 写进表头{'Mean'};
123
- 求标准差
std(A,a) % a=0时为无偏估计,分母为n-1;a=1时为有偏估计,分母为n。默认形式:std(A,0,1);
std(A,a,b) % 增加的形参b是维数,若A是二维矩阵,则b=1表示按列分,b=2表示按行分;若为三维以上,b=i就是增多的一维维数;
123
>> N = std(Score, 1, 2)
N =
0.8165
0.8165
0.8165
>> N = std(Score, 1, 1)
N =
2.6247 2.4495 2.6247
% 将N及表头写入Excel表格中;
>> xlswrite('04score.xlsx', N, 1, 'F2:F4');
>> xlswrite('04score.xlsx', {'std'}, 1, 'F1');
1234567891011121314151617
在Excel中获得文本
>> [Score Header] = xlsread('04score.xlsx')
Score =
1.0000 2.0000 3.0000 2.0000 0.8165
6.0000 5.0000 4.0000 5.0000 0.8165
7.0000 8.0000 9.0000 8.0000 0.8165
Header =
'' 'text1' 'text2' 'text3' 'Mean' 'std'
'a' '' '' '' '' ''
'ab' '' '' '' '' ''
'abc' '' '' '' '' ''
123456789101112131415
3.3.2 低阶文件输入/输出
- 以字节或字符级别读写文件;
- 具有
fid
; - 文件中的位置由可以移动的指针
pointer
指定;
功能函数
函数 | 功能 |
---|---|
fopen | 打开文件 |
fclose | 关闭文件 |
fscanf | 读数据 |
fprintf | 写数据 |
feof | 文件结尾测试 |
fid = fopen('[filename]', '[permission]');
status = fclose(fid);
12
permission
包括'r'
'r+'
'w'
'w+'
'a'
'a+'
举例
将正弦值写入文件
x = 0:pi/10:pi;
y = sin(x);
fid = fopen('sinx.txt','w');
for i=1:11
fprintf(fid,'%5.3f %8.4f\n', x(i), y(i));
end
fclose(fid);
type sinx.txt
12345678
通过格式化I/O进行读写
- Read:
A = fscanf(fid, format, size);
- Write:
fprintf(fid, format, x, y, ...);
A
:读取的数据;format
:格式说明符;size
:读取的数据量;x, y, ...
:写入的数据;- 格式说明符:
%-12.5e
;-12.5
表示宽度和精度;
说明符 | 描述 |
---|---|
%c | |
%d | |
%e | |
%f | |
%g | |
%o | |
%s | |
%u | |
%x |
从文件中读取
- 检查是否是文件的结尾:
feof(fid)
fid = fopen('04asciiData.txt','r'); i = 1;
while ~feof(fid)
name(i,:) = fscanf(fid,'%5c',1);
year(i) = fscanf(fid,'%d',1);
no1(i) = fscanf(fid,'%d',1);
no2(i) = fscanf(fid,'%d',1);
no3(i) = fscanf(fid,'%g',1);
no4(i) = fscanf(fid,'%g\n');
i=i+1;
end
fclose(fid);
至此,整个基础知识学习到此结束,后续不会再进行相关记录。