变量: string, structure, cell
s1 = 'h'
whos
uint16(s1)
运行;
ans =
uint16
104
一、字符串
s1 = 'Example';
s2 = 'String';
s3 = [s1 s2];
s4 = [s1; s2];
运行;
s1 =
'Example'
s2 =
'String'
s3 =
'ExampleString'
错误使用 vertcat
串联的矩阵的维度不一致。
出错
s4 = [s1;s2]
二、结构体 structure
1、当结构体里有多个对象时的索引
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];
2、功能函数
fieldnames(student)
rmfield(student,'id')
三、、cell,array不同类型数据的矩阵
1、用大括号宣告
2、查看内容
- A{1,1} = 内容
- A(1,1)= pointer
- A{1,1}(1,1) 查看{1,1}内容矩阵里的(1,1)位置
3、function
4、多维数组 cell(row,column,layer)
cat()将矩阵在不同方向拼起来
A=[1 2;3 4]
B=[5 6;7 8]
C1=cat(1,A,B)
C2=cat(2,A,B)
C3=cat(3,A,B)
运行:
A =
1 2
3 4
B =
5 6
7 8
C1 =
1 2
3 4
5 6
7 8
C2 =
1 2 5 6
3 4 7 8
C3(:,:,1) =
1 2
3 4
C3(:,:,2) =
5 6
7 8
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.141592653589793] [5×5 double]
C =
1×4 cell 数组
'James Bond' [3.141592653589793] [3×2 double] [5×5 double]
function
五、File Access,将workspace 的数据存到File里,save()and load()
clear;
a = magic(4);
save mydata1.mat
save mydata2.mat -ascii
load('mydata1.mat')
load('mydata2.mat','-ascii')
从excel上读取数据
Score = xlsread('04Score.xlsx')
Score = xlsread('04Score.xlsx', 'B2:D4')
基础绘图
- plot()
- hold on /hold off ,保持多个图形
- plot(x,y,‘str’),改变绘图的style
- legend(),产生图的介绍
- xlabel()
- ylabel()
- title()
- text()
hold on
plot(cos(0:pi/20:2*pi));
plot(sin(0:pi/20:2*pi));
hold off