필터 지우기
필터 지우기

what's a persistent variable????

조회 수: 4 (최근 30일)
mohammad asasi
mohammad asasi 2012년 5월 7일
Hi
really I'm confused by persistent variables!!!
please help me to know what are they and their applications???
  댓글 수: 1
Walter Roberson
Walter Roberson 2012년 5월 7일
Please do not use your own name as a Tag. Tags are intended to reflect categories of questions, so that people can easily find related questions that might be of interest to them. If you need to find your own questions you can use the My Questions link.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 7일
A persistent variable retains its values between calls to the routine.
For example
function trial
persistent times_called
if isempty(times_called); times_called = 0; end %never called before
times_called = times_called + 1;
if mod(times_called,10) == 0
fprintf('Trial called %d times so far\n', times_called);
end
%now do the real work of the routine
.....
end
The above is just one of many uses.
  댓글 수: 1
mohammad asasi
mohammad asasi 2012년 5월 9일
thanks for your answer

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2012년 5월 7일
At first I suggest reading the documentation
doc persistent
help persistent
The you can try it:
function myFunc
persistent V
if isempty(V)
disp('V is empty');
disp('set V to 1');
V = 1;
end
V = V + 1;
fprintf('V = %d\n', V);
Save this file and start it through the command line:
myFunc
myFunc
myFunc
And now clear the function and call it again:
clear myFunc
myFunc
myFunc
Clearer now?
  댓글 수: 1
mohammad asasi
mohammad asasi 2012년 5월 9일
thanks jan for your answer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by