MATLAB 命令
MATLAB 是由 MathWorks 开发的交互式多程序设计语言和数值计算环境。 MATLAB 提供了当用户想要使用命令行界面与任何应用程序交互时将使用的命令。
以下是 MATLAB 中使用的命令列表。
用于管理会话的命令
Command | Use |
---|---|
clc | Clears command window |
clear | Removes the variables from memory |
global | Declares variables globally |
exist | Checks for the existence of a particular file or memory |
help | Searches for a help topic |
quit | Stops MATLAB |
who | Lists current variables in use |
lookfor | Searches help entries for a keyboard |
示例 1
Matlab
% MATLAB Code to illustrate
% global
function setGlobalx(val)
global x
Matlab
% MATLAB Code to illustrate who
x = 2
who
Matlab
% MATLAB Code to illustrate
% clear
clear
type x
Matlab
% MATLAB Code to illustrate
% pwd
pwd
Matlab
% MATLAB Code to illustrate
% date
c = date
Matlab
% MATLAB Code to illustrate
% disp
A = [15 150];
S = 'Hello World.';
disp(A)
disp(S)
Matlab
% MATLAB Code to illustrate
%input
age = input('how old are you: ');
% At this point, the variable: age, will contain
% whatever value the user types
Matlab
% MATLAB Code to illustrate
%cat
array1 = [1,2,3]
array2 = [4,5,6]
cat(1,array1,array2)
Matlab
% MATLAB Code to illustrate
%max
max(array1)
Matlab
%MATLAB Code to illustrate
%min
min(array2)
Matlab
%MATLAB Code to illustrate
%eye
eye(2,2)
Matlab
% MATLAB Code to illustrate
%plot
x = linspace(0,20)
y = cos(x)
plot(x,y)
Matlab
%MATLAB Code to illustrate
%grid
x = linspace(0,20)
y = cos(x)
plot(x,y)
grid on
输出:
x = val;
示例 2
MATLAB
% MATLAB Code to illustrate who
x = 2
who
输出:
Variables in the current scope: x
示例 3
MATLAB
% MATLAB Code to illustrate
% clear
clear
type x
输出:
error: type 'x' is undefined
使用系统的命令
Command | Use |
cd | Changes current directory |
date | Displays current date |
delete | Deletes a file |
dir | Lists all the files in a directory |
path | Displays search path |
pwd | Displays current directory |
type | Displays content of a file |
wklread | Reads.wk1 spreadsheet file |
save | Saves workplace variables in a file |
示例 1
MATLAB
% MATLAB Code to illustrate
% pwd
pwd
输出:
ans = /home/oo
示例 2
MATLAB
% MATLAB Code to illustrate
% date
c = date
输出:
c= '18-jun-2021'
输入和输出命令
Command | Use |
disp | Displays content of an array or a string |
fscanf | Reads formatted data from a file |
format | Controls screen-display format |
fprintf | Performs formatted writes to file or screen |
input | Displays the prompt and waits for input |
; | Suppresses screen printing |
示例 1
MATLAB
% MATLAB Code to illustrate
% disp
A = [15 150];
S = 'Hello World.';
disp(A)
disp(S)
输出:
15 150
Hello World.
示例 2
MATLAB
% MATLAB Code to illustrate
%input
age = input('how old are you: ');
% At this point, the variable: age, will contain
% whatever value the user types
输出:
20
向量、矩阵和数组命令
Command | Use |
cat | Concatenates the array |
find | Finds the indices of nonzero elements |
max | Returns the largest element |
min | Returns the smallest element |
prod | Product of each column |
sort | Sorts each column |
rank | Computes rank of a matrix |
eye | Creates an identity matrix |
inv | Computes the inverse of a matrix |
示例 1
MATLAB
% MATLAB Code to illustrate
%cat
array1 = [1,2,3]
array2 = [4,5,6]
cat(1,array1,array2)
输出:
ans =
1 2 3
4 5 6
示例 2
MATLAB
% MATLAB Code to illustrate
%max
max(array1)
输出:
ans = 3
示例 3
MATLAB
%MATLAB Code to illustrate
%min
min(array2)
输出:
ans = 4
MATLAB
%MATLAB Code to illustrate
%eye
eye(2,2)
输出:
ans =
Diagonal Matrix
1 0
0 1
绘图命令:Commands Use axis Sets axis limit grid Displays grid lines plot Generates xy plot title Puts title at top of the plot close Closes current plot bar Creates bar chart print Prints the plot / saves the plot figure Opens a new figure window Close all Closes all plots
示例 1:
MATLAB
% MATLAB Code to illustrate
%plot
x = linspace(0,20)
y = cos(x)
plot(x,y)
输出:
示例 2:
MATLAB
%MATLAB Code to illustrate
%grid
x = linspace(0,20)
y = cos(x)
plot(x,y)
grid on
输出: