Matlab常用命令。
logical
char
numeric:
int8、uint8、single、double
int16、uint16、
int32、uint32、
int64、uint64
默认数据类型double
cell
struct
Variable
Built-in function
SubFunction
Private function:
MEX-file
P-file
M-file
Style | Example |
---|---|
short | 3.1416 |
long | 3.141592653589793 |
shortE | 3.1416e+00 |
longE | 3.141592653589793e+00 |
bank | 3.14 |
hex | 400921fb54442d18 |
rat | 355/113 |
索引从1开始计数。
A = [1 21 6;
5 17 9;
31 2 7]
A(8) = 9
A([1 3 5]) = 1 31 17
A([1 3;1 3]) = 1 31;1 31
A(3,2) = 2
A([1 3],[1 3]) = 1 6;31 7
1到100
A = 1:100
eye(n):对角线矩阵
zeros(n1,n2):n1n2零矩阵
ones(n1,n2):n1n2全1矩阵
diag():diagonal矩阵
rand():随机矩阵
max(A)
max(max(A))
min(A)
sum(A)
mean(A)
sort(A)
sortrows(A)
size(A)
length(A)
find(A)
if,elseif,else | Execute statements if condition is true |
---|---|
for | Execute statements specified number of times |
switch,case,otherwise | Execute one of several groups of statements |
try,catch | Execute statements and catch resulting errors |
while | Repeat execution of statements while condition is true |
break | Terminate execution of for or while loop |
continue | Pass control to next iteration of for or while loop |
end | Terminate block of code,or indicate last array index |
pause | Halt executionnn temporarily |
return | Return control to invoking function |
Opeerator | Meaning |
---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equal to |
~= | Not Equal to |
&& | And |
ll | Or |
if condition1
statement1
elseif condition2
statement2
else
statement3
end
switch expression
case value1
statement1
case value2
statement2
.
.
otherwise
statement
end
while expression
statement
end
for variable= start:increment:end
commands
end
在while中直接跳出循环到达end
x = 2;k = 0;error = inf;
error_threshold = 1e-32;
while error > error_threshold
if k > 100
break
end
x = x - sin(x)/cos(x);
error = abs(x - pi);
k = k+ 1
end
clear all 清除所有变量
close all 关闭所有图形窗口
;隐藏命令窗口输出
… 换行
Ctrl + C终止运行脚本
自由落体
function x = freebody(x0,v0,t)
x = x0 + v0.*t + 1/2*9.8*t.*t;
function [a,F] = acc(v2,v1,t2,t1,m)
a = (v2-v1)./(t2-t1);
F = m.*a;
inputname | Varialbe name of function inpu |
---|---|
mfilename | File name of currently running function |
nargin | Number of function input arguments |
nargout | Number of function output arguments |
varargin | Variable length input argument list |
varargout | Variable length output argument list |
f = @(x) exp(-2*x);
x = 0:0.1:2;
plot(x,f(x));
函数 | 功能 |
---|---|
double() | 转换成double类型 |
single() | 转换成单精度 |
int8() | 转换成8位符号整型 |
int16() | 转换成16位符号整型 |
int32() | 转换成32位符号整型 |
int64() | 转换成64位符号整型 |
uint8() | 转换成8位无符号整型 |
uint16() | 转换成16位无符号整型 |
uint32() | 转换成32位无符号整型 |
uint64() | 转换成64位无符号整型 |
s1 = 'h'
whos
uint16(s1)
s2 = 'H'
whos
uint16(s2)
s1 = 'Example';
s2 = 'String';
字符串拼接
s3 = [s1 s2];
s4 = [s1;s2];
str = 'aardvark'
'a' == str
1 1 0 0 0 1 0 0
%将a替换成Z
str(str == 'a') = 'Z'
>> student.name = 'John Doe';
>> student.id = '[email protected]';
>> student.number = 301073268;
>> student.grade = [100,74,73;95,91,85.5;100,98,72];
>> student
>
包含以下字段的 struct:
name: 'John Doe'
id: '[email protected]'
number: 301073268
grade: [3×3 double]
>> student(2).name = 'Ann Lane';
>> student(2).id = '[email protected]';
>> student(2).number = 301078853;
>> student(2).grade = [95 100 90 ;95 82 97 ;100 85 100];
>student
> 包含以下字段的 1×2 struct 数组:
name
id
number
grade
函数 | 功能 |
---|---|
cell2struct | Convent cell array to structure array |
fieldnames | Field names of structure,or public fields of object |
getfield | Field of structure array |
isfield | Determine whether inupt is structure array field |
isstruct | Determine whether input is structure array |
orderfields | Order fields of structure array |
rmfield | Remove fields from structure |
setfield | Assign values to structure array field |
struct | Create structure to cell array |
struct2cell | Convert structure to cell array |
structfun | Apply function to each field of scalar structure |
>> A = struct('data',[3 4 7;8 0 1],...
'nest',struct('testnum','Test1','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(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 }
>> A(1,1)
ans =
1×1 cell 数组
{3×3 double}
>> A{1,1}
ans =
1 4 3
0 5 8
7 2 9
函数 | 功能 |
---|---|
cell | Create cell array |
cell2mat | Convert cell array to numeric array |
cell2struct | Convert cell array to structure array |
celldisp | Cell array contents |
cellfun | Apply function to each cell in cell array |
cellplot | Graphically display structure of cell array |
cellstr | Create cell array of strings from character array |
iscell | Determine whether input is cell array |
mat2cell | Convert array to cell array with different sized cells |
num2cell | Convert array to cell array with consistently sized cells |
struct2cell | Convert structure to cell array |
>> 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}