控制命令窗口输入和输出

来源:互联网 发布:网络中二病是什么意思 编辑:程序博客网 时间:2024/05/19 15:41

控制命令窗口输入和输出

迄今为止,你已经使用MATLAB命令行,键入函数和表达式,并且查看打印在命令窗口中的结果。这部分描述了

(1)格式函数

(2)压制输出

(3)输入长语句

(4)命令行编辑

一、格式函数

格式函数控制通过MATLAB显示的变量的数值格式。函数仅影响如何显示数,不会影响MATLAB的计算或保存它们。这里不同的格式,共同输出一个变量X的结果

注意:确认适当间隔,使用一个固定宽,例如书信。

x=[4/31.2345e-6]

短格式

  1.3333   0.0000

e的短格式

  1.3333e+000 1.2345e-006

g的短格式

  1.3333 1.2345e-006

长格式

  1.33333333333333  0.00000123450000

e的长格式

  1.333333333333333e+000   1.234500000000000e-006

g的长格式

  1.33333333333333              1.2345e-006

银行格式

  1.33         0.00

分数格式

  4/3         1/810045

十六进制格式

  3ff5555555555555  3eb4b6231abfd271

如果一个矩阵的最大元素超过10^3或小于10^3,MATLAB为短和长格式应用一个命令尺度因子。

并且格式函数显示在上面

格式简化

在输出中压制空白行的许多应用。这让你在一个屏幕或窗口上看到更多信息。如果你向更多控制输出格式。使用sprintf和fprintf函数。

 

二、压制输出

如果你简单键入一个语句并按返回或确认,MATLAB在屏幕上自动显示结果。不过,如果你用分号作为行的结束,MATLAB执行计算但是不会显示任何输出。当你产生一个大型矩阵时,这是细节的使用。例如

A=magic(100);

三、输入长语句

如果一个语句在一行上不合适,是用一个省略(三个点),...,返回或确认以下表示语句连续在下一行上。例如

s=1-1/2+1/3-1/4+1/5-1/6+1/7...
     -1/8+1/9-1/10+1/11-1/12;

空白间隔周围的=,+和-单精度操作符,但是它们改善了阅读性。</FONT>

四、命令行编辑

各种命令语句可以通过你键盘上的控制键位进行回顾,并且重复使用你更早键入的语句。例如,假设你错误键入

rho=(1+sqt(5))/2

你已经错误键入sqrt。MATLAB回显

Undefinedfunctionorvariable'sqt'.

不能定义函数或变量'sqrt'。

重复键入代替整个行,简单按上方向()键。你键入过的语句重复显示。使用左方向()键去移动光标覆盖和插入缺少的'r'。再次使用上方向()键回顾以前的行。键入一个字符然后用上方向()键查找一个以往的行。你也可以中命令历史中复制命令语句,查看命令历史。

下面是在命令窗口中允许你键入的控制键位。如果你喜欢使用Ctrl+key的键位去命令显示,也可以在偏好设置中进行设置。了解如何设置请查阅在MATLAB开发环境文档中的一般桌面工具的键盘快捷键。

键位控制键操作上方向键Ctrl+P回顾以往键入的命令,仅仅在命令行中有效下方向键Ctrl+N

回顾下一行键入的命令,仅在命令行中,如果你已经使用过上方向键或Ctrl+P。

左方向键Ctrl+B向左移动光标一个字符右方向键Ctrl+F向右移动光标一个字符Ctrl+右方向键 光标移动到一个词的开始Ctrl+左方向键光标移动到一个词的结束HomeCtrl+A光标移动到一行的开始EndCtrl+E光标移动到一行的结束Ctrl+Home 光标移动到一行的最左端Ctrl+EndCtrl+U光标移动到一行的最右端EscCtrl+D删除光标所在的一行DeleteCtrl+H删除光标右边一个字符Backspace删除光标左边的一个字符Ctrl+K删除光标右边的所有字符Shift+Home  选中光标左边的全部字符Shift+End 选中光标右边的全部字符

 

ControllingCommandWindowInputandOutput

Sofar,youhavebeenusingtheMATLABcommandline,typingfunctionsandexpressions,andseeingtheresultsprintedintheCommandWindow.Thissectiondescribes

(1)TheformatFunction

(2)SuppressingOutput

(3)EnteringLongStatements

(4)CommandLineEditing

一、TheformatFunction

TheformatfunctioncontrolsthenumericformatofthevaluesdisplayedbyMATLAB.Thefunctionaffectsonlyhownumbersaredisplayed,nothowMATLABcomputesorsavesthem.Herearethedifferentformats,togetherwiththeresultingoutputproducedfromavectorxwithcomponentsofdifferentmagnitudes.

Note   Toensureproperspacing,useafixed-widthfont,suchasCourier.

x=[4/31.2345e-6]

formatshort

  1.3333   0.0000

formatshorte

  1.3333e+000 1.2345e-006

formatshortg

  1.3333 1.2345e-006

formatlong

  1.33333333333333  0.00000123450000

formatlonge

  1.333333333333333e+000   1.234500000000000e-006

formatlongg

  1.33333333333333              1.2345e-006

formatbank

  1.33         0.00

formatrat

  4/3         1/810045

formathex


  3ff5555555555555  3eb4b6231abfd271

Ifthelargestelementofamatrixislargerthan10^3orsmallerthan10^(-3),MATLABappliesacommonscalefactorfortheshortandlongformats.

Inadditiontotheformatfunctionsshownabove

formatcompact

suppressesmanyoftheblanklinesthatappearintheoutput.Thisletsyouviewmoreinformationonascreenorwindow.Ifyouwantmorecontrolovertheoutputformat,usethesprintfandfprintffunctions.

二、SuppressingOutput

IfyousimplytypeastatementandpressReturnorEnter,MATLABautomaticallydisplaystheresultsonscreen.However,ifyouendthelinewithasemicolon,MATLABperformsthecomputationbutdoesnotdisplayanyoutput.Thisisparticularlyusefulwhenyougeneratelargematrices.Forexample,

A=magic(100);

三、EnteringLongStatements

Ifastatementdoesnotfitononeline,useanellipsis(threeperiods),...,followedbyReturnorEntertoindicatethatthestatementcontinuesonthenextline.Forexample,

s=1-1/2+1/3-1/4+1/5-1/6+1/7...
     -1/8+1/9-1/10+1/11-1/12;

Blankspacesaroundthe=,+,and-signsareoptional,buttheyimprovereadability.

四、CommandLineEditing

Variousarrowandcontrolkeysonyourkeyboardallowyoutorecall,edit,andreusestatementsyouhavetypedearlier.Forexample,supposeyoumistakenlyenter

rho=(1+sqt(5))/2

Youhavemisspelledsqrt.MATLABrespondswith

Undefinedfunctionorvariable'sqt'.

Insteadofretypingtheentireline,simplypressthekey.Thestatementyoutypedisredisplayed.Usethekeytomovethecursoroverandinsertthemissingr.Repeateduseofthekeyrecallsearlierlines.Typingafewcharactersandthenthekeyfindsapreviouslinethatbeginswiththosecharacters.YoucanalsocopypreviouslyexecutedstatementsfromtheCommandHistory.Formoreinformation,seeCommandHistory.

FollowingisthelistofarrowandcontrolkeysyoucanuseintheCommandWindow.IfthepreferenceyouselectforCommandlinekeybindingsisEmacs(MATLABstandard),youcanalsousetheCtrl+keycombinationsshown.SeealsogeneralkeyboardshortcutsfordesktoptoolsintheMATLABDevelopmentEnvironmentdocumentation.

 Key ControlKeyforEmacs(MATLABstandard)Preference Operation  Ctrl+P Recallpreviousline.Worksonlyatcommandline.  Ctrl+N Recallnextline.WorksonlyatcommandlineifyoupreviouslyusedtheuparroworCtrl+P.  Ctrl+B Movebackonecharacter.  Ctrl+F Moveforwardonecharacter. Ctrl+  Moverightoneword. Ctrl+  Moveleftoneword. Home Ctrl+A Movetobeginningofcommandline. End Ctrl+E Movetoendofcommandline. Ctrl+Home  MovetotopofCommandWindow. Ctrl+End  MovetoendofCommandWindow. Esc Ctrl+U Clearcommandline. Delete Ctrl+D Deletecharacteratcursorincommandline. Backspace Ctrl+H Deletecharacterbeforecursorincommandline.  Ctrl+K Cutcontents(kill)toendofcommandline. Shift+Home  Highlighttobeginningofcommandline. Shift+End  Highlighttoendoflastline.CanstartatanylineintheCommandWindow.

原创粉丝点击