Static variables in MATLAB

236 views (last 30 days)
Anirudh Sharma
Anirudh Sharma on 6 Oct 2016
Commented: Ravi Singh on 23 Apr 2020
How do I declare static variable in MATLAB?

Accepted Answer

Abhishek Jain
Abhishek Jain on 6 Oct 2016
Edited: madhan ravi on 11 Nov 2018
Use keyword persistent to declare static variables
persistent x;

More Answers (1)

Adam
Adam on 6 Oct 2016
You can use persistent variables if that is what you mean, but only use them if you really need to. Similar to global variables they are far easier to introduce bugs into your workspace with then simply passing variables around between functions or using a class to define the scope of a variable.
  1 Comment
Ravi Singh
Ravi Singh on 23 Apr 2020
Hi Adam ,
I need to assign a static variable in one method call and use the same variable in another method call is there any way to do so ?

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!