persistent (表面意思是 持续的,连续的)

Define persistent variable  %%声明静态变量

Syntax            %% 语法

persistent X Y Z        %%定义局部变量 X  Y   Z

Description  描述

其实就是定义局部静态变量的语法,变量生命期为全局,但是可见性在其被声明的函数内部,

persistent X Y Z defines X, Y, and Z as variables that are local to the function in which they are declared; yet their values are retained in memory between calls to the function.

定义局部静态变量X Y Z,在函数范围内声明X Y Z,它们的值局限在这个函数内。

Persistent variables are similar to global variables because MATLAB creates permanent storage for both. They differ from global variables in that persistent variables are known only to the function in which they are declared. This prevents persistent variables from being changed by other functions or from the MATLAB command line.

Persistent variables(局部变量)与global variables(全局变量) 很类似,因为matlab为它们都有固定的内存。不同的是Persistent variables仅仅在声明的函数范围内识别。与其他函数的变量或MATLAB 命令行所区别。

Persistent variables are cleared when the M-file is cleared from memory or when the M-file is changed. To keep an M-file in memory until MATLAB quits, use mlock.

当M文件被从内存中清除或当M文件被改变时,Persistent变量被清除。要保持M文件在内存中,请使用mlock

If the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix.

如果Persistent变量在声明时没有赋值,将被初始化为空矩阵。

It is an error to declare a variable persistent if a variable with the same name exists in the current workspace.

如果在当前工作空间中有同名的变量存在,则声明同名的Persistent变量将会报错。

Remarks   备注

There is no function form of the persistent command (i.e., you cannot use parentheses and quote the variable names).

Matlab不允许将Persistent变量声明为函数参数。

Example  举例

This function prompts a user to enter a directory name to use in locating one or more files. If the user has already entered this information, and it requires no modification, they do not need to enter it again. This is because the function stores it in a persistent variable (lastDir), and offers it as the default selection. Here is the function definition:

这个函数提示你输入一串字符定位文件。如果你已经输入了确切信息,就不必要再改变了。

这个函数定义了一个局部变量lastDir,并把默认选择赋值给它lastDir。下面是函数列:

function find_file(file)%%头文件

persistent lastDir  %定义persistent variable

if isempty(lastDir)

prompt = 'Enter directory:  ';      %初始状态为'Enter directory:

else

prompt = ['Enter directory[' lastDir ']:  '];  %默认路径名

end

response = input(prompt, 's');           %输入路径名

if ~isempty(response)

dirName = response;

else

dirName = lastDir;

end

dir(strcat(dirName, file))            %查找文件

lastDir = dirName;                 %赋值

Execute the function twice. The first time, it prompts you to enter the information and does not offer a default:

cd(matlabroot)

find_file('is*.m')

Enter directory:  toolbox/matlab/strfun/

iscellstr.m  ischar.m     isletter.m   isspace.m    isstr.m

isstrprop.m

The second time, it does offer a default taken from the persistent variable dirName:

find_file('is*.m')

Enter directory[toolbox/matlab/strfun/]:  toolbox/matlab/elmat/

isempty.m               isfinite.m              isscalar.m

isequal.m               isinf.m                 isvector.m

isequalwithequalnans.m  isnan.m

matlab中如何定义局部变量,matlab局部变量定义 persistent相关推荐

  1. matlab中错误使用fmincon,MATLAB中fmincon 函数问题

    MATLAB中fmincon 函数问题 Matlab的fmincon优化问题 请问: 各位高手帮忙看看我的程序又什么问题?显示错误 Error in ==> Fun at 33 [w,fval] ...

  2. matlab中rastrigin图形绘制,matlab函数function

    x?6 x?6 (1) 利用MATLAB语言编写S函数.程序如下: function [sys,x0,str,ts]=sfunction(t,x,u,flag) switch flag, case 0 ...

  3. matlab中udt函数,《MATLAB信号处理超级学习手册》——2.5 离散时间信号中的运算...

    本节书摘来自异步社区<MATLAB信号处理超级学习手册>一书中的第2章,第2.5节,作者:MATLAB技术联盟 , 史洁玉著,更多章节内容可以访问云栖社区"异步社区"公 ...

  4. matlab中clock是什么,matlab中的clock

    其值为真 Why 简明的答案 Version MATLAB 版本号 时间和日期 Clock 挂钟 Date 日历 Etime 计时函数 Tic 秒表开始计时 Toc 计时函数 Cputime CPU ...

  5. matlab中水平垂直线,关于Matlab:水平-垂直线

    我是Matlab的新手. 我有一个图像块,如下图所示: 白色显示像素的值等于1,黑色显示像素的值等于0, 我想获取vertical only lines. 这意味着应删除水平线,如下所示: 我也想得到 ...

  6. matlab中ln4怎么表示,matlab里ln怎么表示

    matlab中图中颜色和形状表示_数学_自然科学_专业资料.颜色字符串有'c', 'm', 'y', 'r', 'g', 'b', 'w',和'k'.分别表示青,红紫,黄,红,绿,白...... 上下 ...

  7. matlab中axis函数程序,matlab中axis函数

    % 计算函数值,zz 也是21x21的矩阵 为了方便测试立体绘图,MATLAB 提供了一个 peaks...meshz 可将曲面加上围裙: [x,y,z]=peaks; meshz(x,y,z); a ...

  8. cosh matlab中怎么写,用MATLAB绘制cosh函数

    matlab如何绘制参数函数的图像? clear;clc;s=0:0.1:pi/2;t=0:0.1:3*pi/2;[s,t]=meshgrid(s,t);x=cos(s).*cos(t);y=cos( ...

  9. matlab中linspace的用法,matlab中的一些基本使用方法(持续添加)

    MATLAB中的常用清除命令 1.clc命令:即可清空命令窗口中的内容. 2.clf命令:清除当前figure中的内容. 3.close命令:关闭当前打开的figure图形界面. 4.clear命令: ...

  10. matlab中clear的功能,matlab中clc,close,close all,clear,clear all作用区别

    学习链接 clc:清除命令窗口的内容,对工作环境中的全部变量无任何影响 close:关闭当前的Figure窗口 close all:关闭所有的Figure窗口 clear:清除工作空间的所有变量 cl ...

最新文章

  1. uboot linux内核传递参数,Uboot与Linux之间的参数传递详解
  2. 给SAP Spartacus B2B list增加用户提示信息
  3. php时间比现实时间慢8个小时,关于PHP获取时间比实际时间少8小时的问题
  4. javacurrentmap_Java ConcurrentHashMap.forEach方法代码示例
  5. 如何CLASSPATH下的resource.properties文件进行读写操作?
  6. mysql5.0优势_mysql5.0.1提供视图功能其优势
  7. Socket编程---聊天室终极版-私聊群聊
  8. 98K歌词用计算机按的数字是什么,98k谐音中文歌词
  9. Tomcat日志配置,可结合log4j
  10. ov5640帧率配置_OV5640摄像头开窗大小,输出窗口大小,帧率等设置
  11. SQL join关联三个表或多个表
  12. ps怎么把黑白照片变成彩色?ps把儿童黑白照变彩色教程
  13. python自动化测试面试题大全带答案_Python自动化测试笔试面试题精选
  14. OpenCV色域转换
  15. MFC 执行顺序总结
  16. 孟岩所说的革命到底是什么?
  17. 什么是Java SDK
  18. 100家!第一批5G应用解决方案供应商推荐名录
  19. 【ModBus】modbus之协议的相关知识(1)
  20. TeeChart 4.2021.8.23 for .Net-Crack

热门文章

  1. LCN分布式事务(Java)
  2. 5.用转换器抽取特征
  3. exchange批量创建用户邮箱
  4. 随机数字表法计算机分配,随机数字表法
  5. Gmail上不去怎么办?
  6. 计算机网络吞吐量计算
  7. 微信小程序 自定义组件之《转盘》
  8. html中版权号怎么打,网站底部版权符号怎么打出来
  9. 第七章 微分方程(一)
  10. Python模块大全