error of: Declaring persistent variables in scripts is not supported.

Hey, I'm using a code that written in matlab 2018b. and recently i've downloaded matlab 2021a.
when i try to activate the old code, i get an error in the command window: Declaring persistent variables in scripts is not supported.
so this variable isn't supported the the newer virsions?
what can i do, is there a similar variable for those attributes?

5 Comments

Something else must have changed...I don't recall for sure that ever working.
Turn the script into a function with no arguments -- of course, you'll then need access to whatever it uses that is in the workspace.
"What is the work around for this?"
By definition scripts share the calling workspace. There is an ambiguity that results when considering persistent variables in scripts: they are defined to ensure that a certain variable exists and has the same value between calls... but what happens if the script is called from another location, this means that is has a different workspace. Does this mean that it should use the default value (empty double) or use the value from that other workspace? Either way is an unsatisfactory interpretation.
In contrast, the meaning of persistent in a function is clear: it applies to the workspace of that function.
There is no work-around. You cannot use persistent variables in scripts. (And as far as I know, it never was an option, but I won't try to look back all the way to the point they were introduced.)
Or, say it otherwise. You CAN just learn to use functions, if you absolutely need to use persistent variables. But then you are not using a script, so my first statement will not apply.
In a different Question, @Lacey Fauci indicated that it turned out they had accidentally used funcion instead of function and that once they corrected that, that they were able to use persistent variables as expected.

Sign in to comment.

Answers (1)

persistent variables are stored in a workspace associated with each function. Script files do not have those workspaces. There is no work around other than to create a function

2 Comments

Isn't it a tautology to talk about persistent variables in a script ? Aren't they persistent by definition meaning that they are kept in workspace during the run of the program ?
Variables assigned in a script are stored in the workspace of the invoking function if the script is invoked from a function, and are stored in the base workspace if the script is invoked outside of a function (including some cases where the script is invoked as part of a callback.) The case where the base workspace is being used is effectively persistent until cleared or changed.
If, hypothetically, persistent variables could be declared in a script, then it could lead to some confusion.
test1()
test2()
function test1
ABC
pqr = 123
end
function test2
ABC
pqr
end
%script ABC
persistent pqr
pqr
pqr = -999
If the workspace of the calling function was always being used then this would be equivalent to
test1()
test2()
function test1
persistent pqr
pqr
pqr = -999
pqr = 123
end
function test2
persistent pqr
pqr
pqr = -999
pqr
end
in which case it becomes obvious that the two pqr are not connected, and the assignment of 123 to pqr inside test1 should not affect the pqr in test2 -- so the first time test2 is invoked, pqr inside it should be empty
But it seems to me likely that people would expect that persistent inside a script would make the variable persistent for the script not for whatever function happened to call the script, so people would expect that the second time the script ABC was invoked, that pqr would continue to have the same value that was assigned to it inside the script, so the display of pqr the second time ABC was invoked would probably be expected to show the -999 value that was assigned to pqr the first time ABC was invoked.
Now, certainly Mathworks could define scripts as being permitted to hold persistent state independent of their caller, but that is not permitted at the moment, and leads to confusion. For example if script ABC's pqr were persistent within ABC, then what happens if the invoking function has a non-persistent pqr, is it shadowed within the script? If the invoking function has a persistent pqr, then is it shadowed within the script or does the "persistent" in both locations signal that the value should be shared? How would the script access the function's version of the variable? If a script had a "persistent" variable and such variables are not considered to "shadow" function variables (are shared with functions) and the function clears the variable after execution of the script, then would that affect the value of the persistent variable for other callers of the script ?

Sign in to comment.

Categories

Products

Release

R2021a

Asked:

on 25 Nov 2021

Commented:

on 9 Jul 2023

Community Treasure Hunt

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

Start Hunting!