当前位置:趣知科普网>游戏数码>电脑>

matlab常用函数之format函数short/long/shorE等

电脑 阅读(2.2W)

Matlab的名称来源于“矩阵实验室”,其对矩阵的操作具有先天性的优势(特别是相对于C语言的数组来说),被广泛的应用于科学计算,包括数值微积分、复杂系统的仿真,线性/非线性拟合等等举不胜举。对于初学者,往往对一些矩阵的简单的操作不知道对应的函数是什么,有可能费很大的周折,其实matlab提供了一大堆对矩阵的操作本系列文章旨在对一些Matlab中常用的函数进行介绍。

matlab常用函数之format函数short/long/shorE等

操作方法

(01)matlab中format函数用于控制matlab命令窗口中输出结果的显示方式和位数。format的调用形式为:formatformat typeformat('type')其中第一个表示采用默认值,后面两种的type为具体的显示类型字符串。matlab提供了十几种type,包括short (默认),long,shertE,longE,shortG,longG,shortEng,longEng,+,bank,hex,rat,compact,loose. 注意这些type不分大小写,比图short可以是Short,sHort或SHORT等,format内部会自行进行转换识别。可以用get(0,'FormatSpacing')来查看当前是compact还是loose或者用get(0,'Format')来查看当前的其他形式。下面进行一一举例说明。比如执行下面的代码:clcformat compactget(0,'FormatSpacing')format looseget(0,'FormatSpacing')format shortget(0,'format')format longget(0,'format')

matlab常用函数之format函数short/long/shorE等 第2张

(02)首先来看compact和loose,这两个与其他形式不一样,只控制输出结果显示的间隔,不影响数据显示的位数和精度。用get(0,'FormatSpacing')可以显示当前是compact还是loose.执行下面的代码:clcformat compactA1=100A2=piA3=rand(3,4)format('loose') %等价于format looseA4=100A5=piA6=rand(3,4)可以看出A1 A2 A3之间没有空行,是以compact形式显示,而A3 A4 A5 A6间都有一行空行,以loose形式显示。

matlab常用函数之format函数short/long/shorE等 第3张

(03)下面是short形式,这是matlab的默认显示形式,使matlab以short形式显示的调用方式有formatformat defaultformat('default ')format shortformat('short')5种。matlab帮助文档对short的解释为:Scaled fixed-point format, with 4 digits after the decimalpoint. For example, youare displaying a matrix with a wide range of values, consider using shortG. 简单理解就是保留4位小数。执行下面的代码:clcrng('default')format shorta1=pia2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')

matlab常用函数之format函数short/long/shorE等 第4张
matlab常用函数之format函数short/long/shorE等 第5张

(04)下面是long形式,这是matlab的默认显示形式,使matlab以long形式显示的调用方式有format longformat('long')2种。matlab帮助文档对short的解释为:Scaled fixed-point format with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. 简单理解就是double型浮点数据保留15为小数,single型浮点数据保留7位小数。执行下面的代码:clcrng('default')format longa1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')

matlab常用函数之format函数short/long/shorE等 第6张
matlab常用函数之format函数short/long/shorE等 第7张

(05)下面是shortE形式,这是matlab的默认显示形式,使matlab以shortE形式显示的调用方式有format shortEformat('shortE')2种。matlab帮助文档对short的解释为:Floating-point format, with 4 digits after the decimalpoint. For example, 3.1416e+ger-valued floating-pointnumbers with a maximum of 9 digits are not displayed in scientificnotation.简单理解就是以指数形式显示,浮点数底数保留4为小数,整数值小数小于等于9位不以指数形式显示。执行下面的代码:clcrng('default')format shortea1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')a8_f=single(a8)a14_f=single(a14)

matlab常用函数之format函数short/long/shorE等 第8张
matlab常用函数之format函数short/long/shorE等 第9张

(06)下面是longE形式,这是matlab的默认显示形式,使matlab以longE形式显示的调用方式有format longEformat('longE')2种。matlab帮助文档对short的解释为:Floating-point format, with 15 digits after the decimal pointfor double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+ger-valued floating-point numberswith a maximum of 9 digits are not displayed in scientific notation.简单理解就是以指数形式显示,double浮点数底数保留15为小数,single型浮点数保留7位小数,整数值小数小于等于9位不以指数形式显示。执行下面的代码:clcrng('default')format shortea1=pia1_s=single(pi)a2=1/3a3=rand(3,4)a4=epsa5=realmaxa6=realmina7=intmax('int8')a8=intmin('int8')a9=intmax('int16')a10=intmin('int16')a11=intmax('int32')a12=intmin('int32')a13=intmax('int64')a14=intmin('int64')a8_f=single(a8)a14_f=single(a14)

matlab常用函数之format函数short/long/shorE等 第10张
matlab常用函数之format函数short/long/shorE等 第11张

(07)下面是shortG形式,这是matlab的默认显示形式,使matlab以shortG形式显示的调用方式有format shortGformat('shortG')2种。matlab帮助文档对short的解释为:Fixed- or floating-point, whichever is more readable,with  4 digits after the decimal point. For example, xample 5, for a comparison between this format and ger-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.简单理解就是这个是以short和shortE中最优的形式,整数值小数小于等于9位不以指数形式显示。执行下面的代码(可以比较short和shortG的执行结果):clcformat shortx = [25 56 255 9876899999]format shortexformat shortgx

matlab常用函数之format函数short/long/shorE等 第12张

(08)下面是longG形式,这是matlab的默认显示形式,使matlab以longG形式显示的调用方式有format longGformat('longG')2种。简单理解就是这个是以long和longE中最优的形式显示,这里不再赘述。

(09)下面是shortEng形式和longEng是对应short和long的两种工程计数显示,也就是说指数是3的倍数,我们常说的千、兆等。执行下面的代码:clcformat shortEngget(0,'format')A = 5.123456789;for k=1:10disp(A)A = A * 10;endformat longEngget(0,'format')A = 5.123456789;for k=1:10disp(A)A = A * 10;end可以看出结果的指数是0,3,6,9等

matlab常用函数之format函数short/long/shorE等 第13张
matlab常用函数之format函数short/long/shorE等 第14张

(10)+,bank,hex,rat样式分别表示显示结果的正负号(负数显示"-",正数显示“+”,0显示空),保留两位小数(银行的角和分),十六进制显示和有理数(分数)显示。clcformat +a1=pia2=-pia3=0format banka3=pia4=1/3format hexa5=intmax('int64')a6=intmin('int64')a7=piformat rata8=1/3a10=pi

matlab常用函数之format函数short/long/shorE等 第15张

(11)最后需要注意上面的format函数只是对当前matlab程序的显示进行设置,下次打开matlab还是原来的设置。要是设置永远生效,要在在matlab preference菜单中设置,具体是file-->preference...然后如图

matlab常用函数之format函数short/long/shorE等 第16张