Matlab笔记
1.Function
>> edit(which('mean.m'))
如何构建一个function
构建function目的就是方便以后调用这个方法.
注: 这里使用的是.*
运行结果如下:
function x=freebody(x0,v0,t)
x=x0+v0.*t+1/2*9.8*t.*t;
>> freebody(0,0,10)
ans =
490
>> cos(10)
ans =
-0.8391
function [a F]=acc(v2,v1,t2,t1,m)
a=(v2-v1)./(t2-t1);
F=m.*a;
>> [ACC Force]=acc(20,10,5,4,1)
ACC =
10
Force =
10
默认变量
Function Handles
>> f=@(x) exp(-2*x);
>> x=0:0.1:2;
plot(x,f(x));
2.变量与存储
Variable(Data) Type Conversion
>> A=10
A =
10
>> b=int8(20)
b =
20
①char 字符
输入以下代码,会显示出该变量的类型.
>>s2='h';
whos
uint16(),会把该字符在ASCLL表中的值显示出来.
>> uint16(s1)
ans =
104
②String 字符串
>> s1='example'
s1 =
example
>> s2='String'
s2 =
String
>> s3=[s1 s2]
s3 =
exampleString
>> a='123';
>> b='001';
>> s=[a;b]
s =
123
001
[…]显示结果[…];[.;.]显示结果为矩阵.
③逻辑操作符
>> str='aardsjsiwk'
str =
aardsjsiwk
>> str(4)
ans =
d
>>'a'==str
ans =
1 1 0 0 0 0 0 0 0 0
>> 's'==str
ans =
0 0 0 0 1 0 1 0 0 0
>> str(str=='a')='Z'
str =
ZZrdsjsiwk
④Structure
>> student.name='john';
>> student.sex='boy';
>> student.number='123001';
>> student.grade=[100 99 98 78 89 90];
>> student
student =
name: 'john'
sex: 'boy'
number: '123001'
grade: [100 99 98 78 89 90]
>> student.grade=[100 99 98 78 89 90];
>> student.grade(3)
ans =
98
fieldnames() 结构体中文件的名称,例fieldnames(student)
rmfield() 移除结构体中某一项,例rmfield(student,‘name’)
>> A=struct('data',[1 2 4;5 8 9],'nest',...
struct('testnum','Test 1',...
'xdata',[2 3 4],'ydata',[7 9 7]));
>> A(2).data=[0 9 8;7 8 5];
>> A(2).nest.testnum='Test 2';
>> A(2).nest.xdata=[2 3 4];
>> A(2).nest.ydata=[1 1 1];
>> A.nest
ans =
testnum: 'Test 1'
xdata: [2 3 4]
ydata: [7 9 7]
ans =
testnum: 'Test 2'
xdata: [2 3 4]
ydata: [1 1 1]
⑤Cell Array 分块矩阵
整体矩阵分块的话用**()**;
>> A(1,1)={'This is the first'};
A(1,2)={[5+j*6,4+j*5]};
A(2,1)={[1 2 3;4 5 6;7 8 9]};
A(2,2)={{'Tim','Chris'}};
A
A =
'This is the first' [1x2 double]
[3x3 double] {1x2 cell }
读cell array中的详细内容,用**{}**;
>> B=A{1,1}
B =
This is the first
{}后加(),可以把分块矩阵内容里具体信息显示出来
>> C=A{1,1}(1,1)
C =
T
>> C=A{2,2}(1,1)
C =
'Tim'
magic()、num2cell()、mat2cell()
>> a=magic(3)
b=num2cell(a)
c=mat2cell(a,[1 1 1],3)
a =
8 1 6
3 5 7
4 9 2
b =
[8] [1] [6]
[3] [5] [7]
[4] [9] [2]
c =
[1x3 double]
[1x3 double]
[1x3 double]
>> D=c{3,1}
D =
4 9 2
>> D=c{3,1}(3)
D =
2
>> A{1,1,1}=[1 2;4 5];
A{1,2,1}='Name';
A{2,1,1}=2-4i;
A{2,1,2}=7;
A{1,1,2}='Name2';
A{1,2,2}=3;
A{2,1,2}=0:1:3;
A{2,2,2}=[4 5];
>> A
A(:,:,1) =
[2x2 double] 'Name'
[2.0000 - 4.0000i] {1x2 cell}
A(:,:,2) =
'Name2' [ 3]
[1x4 double] [1x2 double]
⑥Cat()
>> A=[1 2;3 4];
B=[2 3;89 9];
C=cat(3,A,B)
C(:,:,1) =
1 2
3 4
C(:,:,2) =
2 3
89 9
>> C=cat(1,A,B)
C =
1 2
3 4
2 3
89 9
>> C=cat(2,A,B)
C =
1 2 2 3
3 4 89 9
⑦reshape()
>>A={'James Bond',[1 2 3;2 3 4;5 5 6];pi,magic(5)}
C=reshape(A,1,4)
A =
'James Bond' [3x3 double]
[ 3.1416] [5x5 double]
C =
'James Bond' [3.1416] [3x3 double] [5x5 double]
>> A=[1:3;4:6]
A =
1 2 3
4 5 6
>> D=reshape(A,3,2)
D =
1 5
4 3
2 6
File Access存储
load、save、xlsread、xlswrite
save() and load() 存储与读取
xlsread() 读取表格中去掉文字的部分
xlswrite() 把数据写入表格
输出数据
>> Score=xlsread('04Score.xls','B2:D5')
Score =
100 98 98
99 78 90
100 89 88
99 89 80
>> M=mean(Score')';
>> xlswrite('04Score.xls',M,1,'E2:E5');
>> xlswrite('04Score.xls',{'mean'},1,'E1')
输出标题和内容
>> [Score Header]=xlsread('04Score.xls')
Score =
100.0000 98.0000 98.0000 98.6667
99.0000 78.0000 90.0000 89.0000
100.0000 89.0000 88.0000 92.3333
99.0000 89.0000 80.0000 89.3333
Header =
'姓名' '语文' '数学' '英语' 'mean'
'丽丽' '' '' '' ''
'机虎' '' '' '' ''
'积极' '' '' '' ''
'谷歌' '' '' '' ''
File Input/Output
fid
把数据写入文件
从文件中读取数据