Matlab Desktop

MATLAB


不同于一般的程序语言, MATLAB 的桌面软件 Desktop 非常重要, 但仍然不是必需的, 比如可以通过 MATLAB Engine API for PythonVSCode 内设置内置 MATLAB Terminal 取代 Desktop.

MATLAB Desktop (R2020b) includes 3 panels:

  1. Current Folder — Access your files
  2. Command Window — Enter commands at the command line, indicated by the prompt >>
  3. Workspace — Explore data that you create or import from files.

前两个 panels 与一般程序语言的 Command Prompt 区别不大, 重要的是 Workspace, 它代表的是工作内存空间, 显示着项目中所有的数据与变量.

Command Window

  • 与其他语言的交互式窗口一样, MATLAB 的 Command Window 可以/适合运行简单的, 临时性的代码, 并负责显示结果
  • 还可以直接输入 file 执行 Current Folder 中的 file.m 文件
    • 注意不能包含 extension
    • 而且无法在系统 cmd 中利用 matlab file.m 执行文件
    • 还可以用指令 run relative/or/absolute/path.m 执行文件
  • 不含赋值符号的表达式的结果都会赋予给默认变量 ans
    • 此变量也会保存在 Workspace 中
  • 需要在 Command Window 中输入长指令时, 可用 ... 加回车软换行
    • ... 在代码执行时作为空格
    • MATLAB 忽略 ... 后该行代码, 因此后面可加注释
  • 若不想每个命令都显示运算结果, 在命令后加上分号 ; 即可

⭐ 常用命令

commanddescription
format [short]短格式显示 double 数值 (保留小数点后 5 位)
format long短格式显示 double 数值 (保留小数点后 5 位)
format short e短格式科学计数法显示 double 数值
format long e短格式科学计数法显示 double 数值
format bank保留小数点后两位数
format hex表示为 16 进制
format rat表示为近似有理数
format +表示元素的符号
dbstop if error在出错位置停止程序, 并保存所有相关变量
who显示 Workspace 变量名
whos显示 Workspace 变量详细信息
what显示 Current Folder 所有 .m, .mat 文件
clcClear Command Window
clearMatlab Functions - clear
save, loadMatlab Functions - save, load
diaryLog following input and output in Command Window to file diary
cdChange directory
deleteDelete file(s)
helpHelp documents
lookforSearch all MATLAB files for keyword
runRun the file with the exact path
quit, exitExit MATLAB

!! 以上命令并不是只能在 Command Window 输入, 也可以包含在 Matlab M File 内, 只是它们都跟 Command Window 有关

Workspace

  • Workspace 中的变量可以储存为 .mat 文件, 也可以从 .mat 文件导入
    • 若 Workspace 已有 .mat 文件中同名变量, 则导入后覆盖已有变量
    • 可以通过函数 saveload 储存和导入变量 Matlab Functions - save, load
  • 命令(函数) clear 可以清除 Workspace 所有变量
  • 命令 pack 可以整理内存
  • 双击 Workspace 中变量还可以修改变量