在 MATLAB 中清除内存中的项目
在本文中,我们将讨论“从内存中清除项目”,在 MATLAB 中可以使用清除操作来完成。
清除操作用于从内存或当前活动工作区中清除指定的项目。它用于释放系统内存。
Syntax:
clear
clear name1 … nameN
clear -regexp expr1 … exprN
clear(‘a’, ‘b’, ‘c’)
clear([‘a’ ‘b’ ‘c’])
clear functions
clear mex
clear global
clear all
clear keyword
这里,
- clear:这用于从当前工作区中删除所有项目。
- clear name1, ... nameN:这用于从内存中删除诸如 name1 ... nameN 之类的变量。
- clear -regexp expr1 ... exprN:这用于删除与列出的任何正则表达式匹配的所有变量。这仅用于删除变量。
- clear('a', 'b', 'c')和clear(['a' 'b' 'c']):这 如果指定的变量存在于当前函数的本地环境中,则用于删除其变量。
- 清除功能:这仅用于删除未锁定的功能。如果一个函数被锁定或当前正在运行,它不会从内存中清除。
- clear mex:这用于清除内存中的所有 MEX 文件。
- clear global:用于清除所有全局变量。
- clear all:这用于从内存中删除所有变量、函数和 MEX 文件。
- 清除关键字:用于清除关键字指示的项目。
示例 1:
Matlab
% MATLAB code for removes all the
% items so its output is blank.
% Initializing some items
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear operation to
% clear all the above specified items
clear
% Getting the remaining items
whos
Matlab
% MATLAB code for Clear operation
% Initializing some items
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear operation to
% clear B and D items
clear B D;
% Getting the remaining items
whos
Matlab
% MATLAB code for clear expression values
% Initializing some expressions
exp1 = 'ab+c';
exp2 = 'a-c';
exp3 = '+/gfg';
% Calling the clear operation to
% clear expression exp2
clear -regexp ^exp2;
% Getting the remaining expressions
whos
Matlab
% MATLAB code for clear()
% Initializing some items
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear function
% to clear items A and C
clear ('A', 'C');
% Again calling the clear function
% to clear D items
clear (['D']);
% Getting the remaining items B
whos
Matlab
% MATLAB code for clear global variable data
% Initializing some items
global A;
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear function
% to clear global items only
clear global
% Getting the remaining items
whos
Matlab
% MATLAB code for clear value of variable
% Initializing some items
global A;
A = 2;
B = 4;
keyword = 6;
D = 8;
% Calling the clear function
% to clear keyword items only
clear keyword
% Getting the remaining items
whos
Matlab
% MATLAB code for all types of items are
% cleared so nothing is printed as output.
% Initializing some items
global A;
A = 2;
B = 4;
keyword = 6;
D = 8;
% Calling the clear function
% to clear all items
clear all
% Getting the remaining items
whos
此示例将清除所有值。
示例 2:
MATLAB
% MATLAB code for Clear operation
% Initializing some items
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear operation to
% clear B and D items
clear B D;
% Getting the remaining items
whos
输出:
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
A 1x1 8 double
C 1x1 8 double
Total is 2 elements using 16 bytes
示例 3:
MATLAB
% MATLAB code for clear expression values
% Initializing some expressions
exp1 = 'ab+c';
exp2 = 'a-c';
exp3 = '+/gfg';
% Calling the clear operation to
% clear expression exp2
clear -regexp ^exp2;
% Getting the remaining expressions
whos
输出:
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
exp1 1x4 4 char
exp3 1x5 5 char
Total is 9 elements using 9 bytes
示例 4:
MATLAB
% MATLAB code for clear()
% Initializing some items
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear function
% to clear items A and C
clear ('A', 'C');
% Again calling the clear function
% to clear D items
clear (['D']);
% Getting the remaining items B
whos
输出:
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
B 1x1 8 double
Total is 1 element using 8 bytes
示例 5:
MATLAB
% MATLAB code for clear global variable data
% Initializing some items
global A;
A = 2;
B = 4;
C = 6;
D = 8;
% Calling the clear function
% to clear global items only
clear global
% Getting the remaining items
whos
输出:
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
B 1x1 8 double
C 1x1 8 double
D 1x1 8 double
Total is 3 elements using 24 bytes
示例 6:
MATLAB
% MATLAB code for clear value of variable
% Initializing some items
global A;
A = 2;
B = 4;
keyword = 6;
D = 8;
% Calling the clear function
% to clear keyword items only
clear keyword
% Getting the remaining items
whos
输出:
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
g A 1x1 8 double
B 1x1 8 double
D 1x1 8 double
Total is 3 elements using 24 bytes
示例 7:
MATLAB
% MATLAB code for all types of items are
% cleared so nothing is printed as output.
% Initializing some items
global A;
A = 2;
B = 4;
keyword = 6;
D = 8;
% Calling the clear function
% to clear all items
clear all
% Getting the remaining items
whos
此示例将清除所有值。