clear 函数

clear 函数

Remove items from workspace, freeing up system memory

清除workspac(工作空间)的条目,释放系统内存。

Graphical Interface

图表界面

As an alternative to the clear function, use Clear Workspace in the MATLAB desktop Edit menu.

有一个可替代clear功能的是,使用MATLAB桌面上Edit(编辑)菜单中的Clear Workspace命令。

Syntax

语法

clear

clear name

clear name1 name2 name3 ...

clear global name

clear -regexp expr1 expr2 ...

clear global -regexp expr1 expr2 ...

clear keyword

clear('name1','name2','name3',...)

Description

描述

clear removes all variables from the workspace. This frees up system memory.

清除workspace(工作空间)的所有值。并释放系统内存。

clear name removes just the M-file or MEX-file function or variable name from the workspace. You can use wildcards (*) to remove items selectively. For example, clear my* removes any variables whose names begin with the string my. It removes debugging breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. If name is global, it is removed from the current workspace, but left accessible to any functions declaring it global. If name has been locked by mlock, it remains in memory. 

Clear name 仅仅删除workspaceM-file 或 MEX-file函数或者是值的名称。你能够使用wildcards(*)来删除选择的条目。举一个例子,clear my*删除任何以my为开头的值的名称。可清除顽固的M-file中的值。如果名称是系统全局性的,那么就在workspace(工作空间)中删除,如果名称已经被m文件锁锁住,其将会在内存中被删除。

Use a partial path to distinguish between different overloaded versions of a function. For example, clear polynom/display clears only the display method for polynom objects, leaving any other implementations in memory.

使用偏路径来区分居于不同过载值之间的函数。举一个例子,clear polynom/display 仅仅删除了显示方法的多项对象,剩余任何其他项在内存中执行。

clear name1 name2 name3 ... removes name1, name2, and name3 from the workspace.

Clear name1 name2 name3 ... 删除workspace(工作空间)中的name1name2,和name3

clear global name removes the global variable name. If name is global, clear name removes name from the current workspace, but leaves it accessible to any functions declaring it global. Use clear global name to completely remove a global variable.

Clear gloabl name 删除

clear -regexp expr1 expr2 ... clears all variables that match any of the regular expressions expr1, expr2, etc. This option only clears variables.

clear global -regexp expr1 expr2 ... clears all global variables that match any of the regular expressions expr1, expr2, etc.

clear keyword clears the items indicated by keyword.

Keyword

Items Cleared

all

Removes all variables, functions, and MEX-files from memory, leaving the workspace empty. Using clear all removes debugging breakpoints in M-files and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared. When issued from the Command Window prompt, also removes the Java packages import list

Classes

The same as clear all, but also clears MATLAB class definitions. If any objects exist outside the workspace (for example, in user data or persistent variables in a locked M-file), a warning is issued and the class definition is not cleared. Issue a clear classes function if the number or names of fields in a class are changed. 

Functions

Clears all the currently compiled M-functions and MEX-functions from memory. Using clear function removes debugging breakpoints in the function M-file and reinitializes persistent variables, since the breakpoints for a function and persistent variables are cleared whenever the M-file is changed or cleared.

Global

Clears all global variables from the workspace.

Import

Removes the Java packages import list. It can only be issued from the Command Window prompt. It cannot be used in a function.

Java

The same as clear all, but also clears the definitions of all Java classes defined by files on the Java dynamic class path (see The Java Class Path in the External Interfaces documentation) . If any java objects exist outside the workspace (for example, in user data or persistent variables in a locked M-file), a warning is issued and the Java class definition is not cleared. Issue a clear java command after modifying any files on the Java dynamic class path.

variables

Clears all variables from the workspace.

clear('name1','name2','name3',...) is the function form of the syntax. Use this form when the variable name or function name is stored in a string

Remarks

When you use clear in a function, it has the following effect on items in your function and base workspaces:

·clear name--If name is the name of a function, the function is cleared in both the function workspace and in your base workspace. 

·clear functions--All functions are cleared in both the function workspace and in your base workspace. 

·clear global--All global variables are cleared in both the function workspace and in your base workspace. 

·clear all--All functions, global variables, and classes are cleared in both the function workspace and in your base workspace. 

Limitations

clear does not affect the amount of memory allocated to the MATLAB process under UNIX.

The clear function does not clear Simulink models. Use close instead.

Examples

Given a workspace containing the following variables

 Name       Size         Bytes  Class

  c          3x4           1200  cell array

  frame      1x1                 java.awt.Frame

  gbl1       1x1              8  double array (global)

  gbl2       1x1              8  double array (global)

  xint       1x1              1  int8 array

you can clear a single variable, xint, by typing

clear xint

To clear all global variables, type

clear global

whos

  Name      Size         Bytes  Class

  c         3x4           1200  cell array

  frame     1x1                 java.awt.Frame

Using regular expressions, clear those variables with names that begin with Mon, Tue, or Wed:

clear('-regexp', '^Mon|^Tue|^Wed');

To clear all compiled M- and MEX-functions from memory, type clear functions. In the case shown below, clear functions was unable to clear one M-file function from memory, testfun, because the function is locked.

clear functions           % Attempt to clear all functions.

inmem

ans = 

    'testfun'             % One M-file function remains in memory.

mislocked testfun

ans =

     1                    % This function is locked in memory.

Once you unlock the function from memory, you can clear it.

munlock testfun

clear functions

inmem

ans = 

   Empty cell array: 0-by-1

### Matlab 中 `clear` 函数的使用说明 在 MATLAB 中,`clear` 是用于删除工作区中的变量或其他对象的重要命令。以下是关于该函数的一些关键点: #### 基本功能 `clear` 的主要作用是从内存中移除指定的对象或清除特定类型的项目。这有助于释放内存资源并管理当前的工作环境[^2]。 #### 语法形式 以下是几种常见的 `clear` 函数调用方式及其含义: 1. **清除非永久性变量** 调用无参数的 `clear` 将从当前工作空间中删除所有非永久性的变量。 ```matlab clear ``` 2. **按名称清理变量** 可以通过提供具体的名字来删除单个或者多个指定的变量。 ```matlab clear variableName ``` 这里 `variableName` 表示要被清除的具体变量名[^3]。 3. **通配符支持** 支持使用通配符 * 和 ? 来匹配一系列相似命名模式下的变量。 ```matlab clear var* % 清理名字以 'var' 开头的所有变量 clear myVar? % 清理名字为 'myVar?' 形式的变量 (其中 '?' 替代任意单一字符) ``` 4. **清理全局变量** 若要专门针对全局变量执行清理操作,则可以附加关键字 global 到指令后面。 ```matlab clear global ``` 5. **其他特殊用途** - 删除 MEX 文件加载到内存中的实例:`clear mexFunction` - 移除已编译伪码版本的脚本/函数以便重新解析它们最新修改后的源代码:`clear functions` 或者更精确地指向某个具体的函数文件如 `clear functionName.m`. #### 示例演示 下面给出几个实际应用的例子帮助理解如何运用此命令: ```matlab % 创建一些测试数据 A = magic(5); B = randn(10,1); % 查看现有变量列表 whos % 单独清除 A 数组 clear A; % 验证剩余变量状态 whos B; ``` 上述例子展示了创建两个矩阵之后查看其存在情况以及随后仅保留其中一个的过程。 #### 注意事项 虽然频繁地利用 `clear all` 可能看似方便快捷地重置整个计算环境,但在大型程序开发过程中应谨慎行事以免误删重要中间结果;推荐养成良好习惯只针对性处理不再需要的数据项从而维持高效稳定运行效率的同时减少不必要的错误风险[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值