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

  • 原创
  • |
  • 浏览:32316
  • |
  • 更新:
  • |
  • 标签:matlab 

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

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

工具/原料

  • matlab

方法/步骤

  1. 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. 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. 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 decimal point. For example, 3.1416.If you are 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. 4

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

    format long

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

    执行下面的代码:

    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. 5

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

    format shortE

    format('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位不以指数形式显示。

    执行下面的代码:

    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. 5
    本页面非法爬取自百度经验
  7. 6

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

    format longE

    format('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位不以指数形式显示。

    执行下面的代码:

    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等
  8. 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.

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

    clc

    format short

    x = [25 56 255 9876899999]

    format shorte

    x

    format shortg

    x

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

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

    format longG

    format('longG')

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

  10. 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等
  11. 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等
  12. 11

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

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

注意事项

  • 如果觉得写的好或者有用的话,恳求动下手指点击页面左下角的【大拇指】进行点赞,然后点击页面下方的【投票】并【评论】,可以【收藏】以便以后观看,您的举手之劳能给予作者莫大的帮助,也可以【关注】我浏览其他精彩经验,谢谢支持!
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。
展开阅读全部
相关标签matlab