第一篇
Matlab中高频操作命令
命令 | 功能 |
---|---|
cd | 显示工作目录 |
clc | 清空当前命令窗口 |
clear | 清空当前工作区内的变量 |
dir | 显示当前目录下的文件 |
disp | 显示变量或文字的内容 |
quit | 退出Matlab |
Matlab中的基本函数
一、 取整函数
取整 | 作用 |
---|---|
round(x) | 此函数是四舍五入 |
fix(x) | 靠0取整 fix(1.1)=1 fix(-1.14)=-1 |
floor(x) | 对x是向下取整 |
ceil(x) | 对x是向上取整 |
二、 Format基本用法
取整 | 作用 |
---|---|
format short | 默认是 short,默认保留5位有效数字 |
format long | 1.双精度类型,小数点后保留15位 2.单精度类型,小数点后保留7位 |
format hex | 十六进制显示 |
format bank | 保留小数点后两位(个人理解) |
format rational | 以分子、分母形式显示 |
三、 get 和 set函数
1.get(对象,‘属性’):返回对象的某个属性对应的值。比如:get(A,‘color’) 返回A的颜色值
2.set(对象,‘属性’,‘属性值’)
比如:set(A,‘linewidth’,2) 将A的线宽设置为2(这样一看,plot对线宽、颜色、线格式的设置是不是就可以理解了呢)
set(A, ‘Color’ , ‘r’ , ‘Linewidth’ ,2, ‘LinStyle’ , ‘–’ )
set(对象,‘属性1’,‘属性值1’,‘属性2’,‘属性值2’,‘属性3’,‘属性值3’)
Function Handle(函数句柄,相当于函数的直接调用)
// An highlighted block
f1=@cos
t=0:pi/5:pi
f1(t)
f2=@complex
f2(3,4)
@cos:代表直接调用了函数余弦cos
f1(t)的输出为: 1.0000 0.8090 0.3090 -0.3090 -0.8090 -1.0000@complex:代表直接调用了复数函数
f2(3,4)的输出则为 3 + 4i
cell(元胞数组)
和Python中的dict()有点相似
dict(<键>:<键值>,<键>:<键值>)
ceil{‘值1’,’值2‘}
// An highlighted block
c={'中国','China';[1 2 3 4 5],100}
celldisp(c) % 将cell中的每个数据display
celldisp(c,'mycell') %对元胞c重命名为mycell
celldisp(c) %displays the structure of a cell array
sturct,结构体
1.创建结构体
// An highlighted block
s1=struct('type',{'big','little'},'color',{'red'},'data',{[1 2 3;2 3 4] [1:4]})
s2=struct([]}) % creates an empty structure.
rmfield(s1,'color') %移除s1中的color
Matlab中标点符号用法
符号 | 定义 |
---|---|
; | 行分隔符号 |
, | 列分隔符 |
: | 生成等差数列 |
[ ] | 定义矩阵 |
{ } | 定义cell |
. | 访问结构体 |
’ ’ | 定义字符串 |
% | 注释 |
Matlab中数据类型
1.整数
整数类型 | 描述 |
---|---|
uint8 | 8bit 无符号整数,范围(0, 2 8 2^8 28-1) |
uint16 | 16bit 无符号整数,范围(0, 2 16 2^{16} 216-1) |
uint32 | 32bit 无符号整数,范围(0, 2 32 2^{32} 232-1) |
uint64 | 64bit 无符号整数,范围(0, 2 64 2^{64} 264-1) |
int8 | 8bit 有符号整数,范围(- 2 8 2^8 28, 2 8 2^8 28-1) |
int16 | 16bit 有符号整数,范围(- 2 16 2^{16} 216, 2 16 2^{16} 216-1) |
int32 | 32bit 有符号整数,范围(- 2 32 2^{32} 232, 2 32 2^{32} 232-1) |
int64 | 64bit 有符号整数,范围(- 2 64 2^{64} 264, 2 64 2^{64} 264-1) |
2.浮点数,matlab中默认的数据为double
复数类型 | 描述 |
---|---|
double | 双精度浮点数 |
single | 单精度浮点数 |
3.复数
复数类型 | 描述 |
---|---|
complex(a) | 实部为a,虚部为0 |
complex(a,b) | 实部为a,虚部为b |
4.与数据类型相关的函数
函数 | 描述 |
---|---|
isnumeric | 判断是否为数值型数据,是则为1 |
isinteger | 判断是否为整数型数据,是则为1 |
isfloat | 判断是否为浮点型数据,是则为1 |
isa(x,‘type’) | 判断x是否为’type’追定的类型,是则为1 |
cast(x,‘type’) | 设置x的类型为’type’ |