在MATLAB中清除内存中的项目
在这篇文章中,我们将讨论 “清除内存中的项目”,在MATLAB中可以使用清除操作来完成。
清除操作是用来从内存或当前活动的工作区中清除指定的项目。它是用来释放系统内存的。
语法:
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 functions 。这仅用于清除未锁定的功能。如果一个函数被锁定或当前正在运行,它不会从内存中被清除。
- clear mex:这是用来清除内存中的所有MEX文件。
- clear global:这是用来清除所有全局变量的。
- clear all。这是用来从内存中删除所有的变量、函数和MEX文件。
- clear keyword:用于清除关键字所指示的项目。
示例 1:
% 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
这个例子将清除所有的值。
示例 2:
% 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 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 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 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 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 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
这个例子将清除所有的值。