필터 지우기
필터 지우기

clear persistent variable in subfunctions

조회 수: 16 (최근 30일)
Torsten Reh
Torsten Reh 2021년 12월 1일
답변: Monika Jaskolka 2021년 12월 1일
Hi,
i have a script which repeadetly calls a subfunction. In that subfunction i have a persistent variable that grows by 1 everytime its called.
after im done i want to reset the persistent variable inside the subfunction.
fun1
fun1
fun1
clear fun1
fun1
function fun1
persistent a
if isempty(a); a = 0; end
a = a+1
end
the output should be: 1-2-3-1
but it is 1-2-3-4, and when i run the script again it is 5-6-7-8.
I do NOT want to use:
clear all
clear functions
I just want to clear the persistent varibale inside that specific funtion.
I reckon i could pass a flag into fun1 to initialize it, but i believe there must be a better way to do this.

답변 (1개)

Monika Jaskolka
Monika Jaskolka 2021년 12월 1일
"MATLAB clears persistent variables when you clear or modify a function that is in memory. "
So it doesn't seem like there is a way to do what you are asking for.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by