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

2025-11-05 10:53:13

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')来查看当前的其他形式。下面进行一一举例说明。比如执行下面的代码:

clc

format compact

get(0,'FormatSpacing')

format loose

get(0,'FormatSpacing')

format short

get(0,'format')

format long

get(0,'format')

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

2、首先来看compact和loose,这两个与其他形式不一样,只控制输出结果显示的间隔,不影响数据显示的位数和精度。用get(0,'FormatSpacing')可以显示当前是compact还是loose.执行下面的代码:

clc

format compact

A1=100

A2=pi

A3=rand(3,4)

format('loose') %等价于format loose

A4=100

A5=pi

A6=rand(3,4)

可以看出A1 A2 A3之间没有空行,是以compact形式显示,而A3 A4 A5 A6间都有一行空行,以loose形式显示。 

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

3、下面是short形式,这是matlab的默认显示形式,使matlab以short形式显示的调用方式有

format

format default 

format('default ')

format short

format('short')

5种。matlab帮助文档对short的解释为:Scaled fixed-point format, with 4 digits after the decimalpoint. For example, 3.1416.If youare displaying a matrix with a wide range of values, consider using shortG. 简单理解就是保留4位小数。

执行下面的代码:

clc

rng('default')

format short

a1=pi

a2=1/3

a3=rand(3,4)

a4=eps

a5=realmax

a6=realmin

a7=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 long

format('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位小数。

执行下面的代码:

clc

rng('default')

format long

a1=pi

a1_s=single(pi)

a2=1/3

a3=rand(3,4)

a4=eps

a5=realmax

a6=realmin

a7=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 shortE

format('shortE')

2种。matlab帮助文档对short的解释为:

Floating-point format, with 4 digits after the decimalpoint. For example, 3.1416e+000.

Integer-valued floating-pointnumbers with a maximum of 9 digits are not displayed in scientificnotation.

简单理解就是以指数形式显示,浮点数底数保留4为小数,整数值小数小于等于9位不以指数形式显示。

执行下面的代码:

clc

rng('default')

format shorte

a1=pi

a1_s=single(pi)

a2=1/3

a3=rand(3,4)

a4=eps

a5=realmax

a6=realmin

a7=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形式显示的调用方式有

format longE

format('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+000.

Integer-valued floating-point numberswith a maximum of 9 digits are not displayed in scientific notation.

简单理解就是以指数形式显示,double浮点数底数保留15为小数,single型浮点数保留7位小数,整数值小数小于等于9位不以指数形式显示。

执行下面的代码:

clc

rng('default')

format shorte

a1=pi

a1_s=single(pi)

a2=1/3

a3=rand(3,4)

a4=eps

a5=realmax

a6=realmin

a7=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形式显示的调用方式有

format shortG

format('shortG')

2种。matlab帮助文档对short的解释为:

Fixed- or floating-point, whichever is more readable,with  4 digits after the decimal point. For example, 3.1416.

SeeExample 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的执行结果):

clc

format short

x = [25 56 255 9876899999]

format shorte

x

format shortg

x

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

8、下面是longG形式,这是matlab的默认显示形式,使matlab以longG形式显示的调用方式有

format longG

format('longG')

2种。简单理解就是这个是以long和longE中最优的形式显示,这里不再赘述。

9、下面是shortEng形式和longEng是对应short和long的两种工程计数显示,也就是说指数是3的倍数,我们常说的千、兆等。执行下面的代码:

clc

format shortEng

get(0,'format')

A = 5.123456789;

for k=1:10

   disp(A)

   A = A * 10;

end

format longEng

get(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显示空),保留两位小数(银行的角和分),十六进制显示和有理数(分数)显示。

clc

format +

a1=pi

a2=-pi

a3=0

format bank

a3=pi

a4=1/3

format hex

a5=intmax('int64')

a6=intmin('int64')

a7=pi

format rat

a8=1/3

a10=pi

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

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

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

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢