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

时间:2024-10-15 23:41:02

1、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、首先来看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、下面是short形式,这是matlab的默认显示形式,使matlab以short形式显示的调用方式有formatformat defaultformat('default')format shortformat('short')5种。matlab帮助文档对short的解释为:Scaled fixed-point format, with 4 digits after the decimal point. For example, 3.1416.If you are 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等
matlab常用函数之format函数short/long/shorE等

4、下面是long形式,这是matlab的默认显示形式,使matlab以long形式显示的调用方式有format longformat('long')2种。matlab帮助文档对short的解释为:Scaled fixed-point format with 15 digits after the decimal point for 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等
matlab常用函数之format函数short/long/shorE等

5、下面是shortE形式,烂瘀佐栾这是matlab的默认显示形式,使matlab以shortE形式显示的调用方式有format shortEformat('shortE')2种。matlab帮助文档对short的解释为:Floating-point format, with 4 digits after the decimal point. For example, 3.1416e+000.Integer-valued floating-point numbers with a maximum of 9 digits are not displayed in scientific notation.简单理解就是以指数形式显示,浮点数底数保留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等
matlab常用函数之format函数short/long/shorE等

6、下面是longE形式,这是matlab的默认显示形式,使matlab以longE形式显示的调用方式有formatlongEformat('longE')2种。matlab帮助文嘲汲萝镗档对short的解释为:Floating-point format, with 15 digits after the decimal point for double; and 7 digits after the decimal point for single. For example, 3.141592653589793e+000.Integer-valued floating-point numbers with 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等
matlab常用函数之format函数short/long/shorE等

7、下面是shortG形式,这是matlab的默认显示形式,使matlab以shortG形式显示的调用方式有formatshortGformat('shortG')2种。matlab帮助文档对short的解释为:Fixed- or floating-point, whichever is more readable, with 4 digits after the decimal point. For example, 3.1416.See Example 5, for a comparison between this format and short.Integer-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等

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

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

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

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等

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

matlab常用函数之format函数short/long/shorE等
© 手抄报圈