matlab中的persistent类型变量

来源:互联网 发布:温州动车事故 知乎 编辑:程序博客网 时间:2024/04/30 07:24

persistent - Define persistent variable

Syntax

persistent 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. Persistent variables are similar to global variables because the MATLAB software 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.

Whenever you clear or modify a function that is in memory, MATLAB also clears all persistent variables declared by that function. To keep a function in memory until MATLAB quits, use mlock.

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

It is an error to declare a variable persistent if a variable with the same name exists in the current workspace. MATLAB also errors if you declare any of a function's input or output arguments as persistent within that same function. For example, the following persistent declaration is invalid:

function myfun(argA, argB, argC)persistent argB

Remarks

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

Example

This function writes a large array to a spreadsheet file and then reads several rows from the same file. Because you only need to write the array to the spreadsheet one time, the program tests whether an array can be read from the file and, if so, does not waste time in repeating that task. By defining the dblArray variable as persistent, you can easily check whether the array has been read from the spreadsheet file.

Here is the arrayToXLS function:

function arrayToXLS(A, xlsfile, x1, x2)persistent dblArray;if isempty(dblArray)     disp 'Writing spreadsheet file ...'    xlswrite(xlsfile, A);enddisp 'Reading array from spreadsheet ...'dblArray = xlsread(xlsfile, 'Sheet1', [x1 ':' x2])fprintf('\n');

Run the function three times and observe the time elapsed for each run. The second and third run take approximately one tenth the time of the first run in which the function must create the spreadsheet:

largeArray = rand(4000, 200);tic,  arrayToXLS(largeArray, 'myTest.xls','E254', 'J256'),  tocWriting spreadsheet file ...Reading array from spreadsheet ...dblArray =    0.0982    0.3783    0.1264    0.7880    0.1902    0.5811    0.2251    0.2704    0.5682    0.7271    0.8028    0.2834    0.6453    0.5568    0.8254    0.4961    0.9096    0.5402Elapsed time is 8.990525 seconds.tic,  arrayToXLS(largeArray, 'myTest.xls','E257', 'J258'),  tocReading array from spreadsheet ...dblArray =    0.4620    0.3781    0.6386    0.5930    0.0946    0.4865    0.1605    0.1251    0.8709    0.5188    0.6702    0.2138Elapsed time is 0.912534 seconds.tic,  arrayToXLS(largeArray, 'myTest.xls','E259', 'J262'),  tocReading array from spreadsheet ...dblArray =    0.7015    0.6588    0.4023    0.0359    0.4512    0.6097    0.1308    0.6441    0.0431    0.6396    0.7481    0.8688    0.8278    0.2686    0.5475    0.8550    0.5896    0.1080    0.9437    0.1671    0.0505    0.1203    0.2461    0.7306Elapsed time is 0.928843 seconds.

Now clear the arrayToXLS function from memory and observe that running it takes much longer again:

clear functionstic,  arrayToXLS(largeArray, 'myTest.xls','E263', 'J264'),  tocWriting spreadsheet file ...Reading array from spreadsheet ...dblArray =    0.6292    0.7788    0.0732    0.6481    0.9299    0.8631    0.7700    0.5181    0.9805    0.5092    0.8658    0.4070Elapsed time is 7.603461 seconds.
这种类型的变量和c语言中的静态变量相类比,都具有全局的性质,但要使用这个变量仅限于申明该变量类型的m文件内,也就是其他的文件和函数无法调用该变量,该变量在m文件在内存中注销或者发生变化时被注销。
 
阅读(1165) | 评论(0) | 转发(0) |
0

上一篇:matlab中strel()与imerode()的使用

下一篇:matlab中标注连接分量

相关热门文章
  • 网站设计:复杂产品的响应式设...
  • 优游娱乐平台 时时彩玩法图解-...
  • 卢松松:38岁老男孩个人建站方...
  • ubuntu sysklogd之配置文件介...
  • JavaScript的数据类型详细解说...
  • 承接自动化测试培训、外包、实...
  • Solaris PowerTOP 1.0 发布
  • For STKMonitor
  • 项目小体会
  • 不用学的汉字输入法 智能H3输...
  • 欢迎qq159135在ChinaUnix博客...
  • 欢迎szhsfw在ChinaUnix博客安...
  • 欢迎seowping在ChinaUnix博客...
  • 欢迎QuickLock在ChinaUnix博客...
  • 3. DB2 Control Center
给主人留下些什么吧!~~
原创粉丝点击