📜  MATLAB 命令

📅  最后修改于: 2022-05-13 01:58:43.756000             🧑  作者: Mango

MATLAB 命令

MATLAB 是由 MathWorks 开发的交互式多程序设计语言和数值计算环境。 MATLAB 提供了当用户想要使用命令行界面与任何应用程序交互时将使用的命令。

以下是 MATLAB 中使用的命令列表。

用于管理会话的命令

Command 

Use

clcClears command window
clearRemoves the variables from memory
globalDeclares variables globally
existChecks for the existence of a particular file or memory
helpSearches for a help topic
quitStops MATLAB
whoLists current variables in use
lookforSearches 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
cdChanges current directory
date Displays current date
deleteDeletes a file
dirLists all the files in a directory
pathDisplays search path
pwdDisplays current directory
typeDisplays content of a file
wklreadReads.wk1 spreadsheet file
saveSaves 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
dispDisplays content of an array or a string
fscanfReads formatted data from a file
formatControls screen-display format
fprintfPerforms formatted writes to file or screen 
inputDisplays 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
findFinds the indices of nonzero elements
max Returns the largest element
minReturns the smallest element
prodProduct of each column
sortSorts each column
rankComputes rank of a matrix
eyeCreates 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
axisSets axis limit
gridDisplays grid lines
plotGenerates xy plot
title Puts title at top of the plot
close Closes current plot
barCreates bar chart
print Prints the plot / saves the plot
figureOpens a new figure window
Close allCloses 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

输出: