定义局部变量,转《matlab局部变量定义 persistent》

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 _disibledevent=>
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
Tags:  定义局部变量

延伸阅读

最新评论

发表评论