Using the persistent function
28 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Yewande Oni
am 7 Sep. 2015
Bearbeitet: Stephen23
am 8 Sep. 2015
Hiya
I am trying to use 'persistent' to use a variable from one function to another. However, when I run my function (which is plotting the said variable), it says the plot is empty. Even though I can see the variable in the workspace with the data. Do I need to write a line of code to tell my function to pull on the data from the workspace?
Thanks
1 Kommentar
Akzeptierte Antwort
Guillaume
am 7 Sep. 2015
A persistent variable is local to one function and one function only. It cannot be shared with another function. It just means that the variable value is preserved between calls to the function.
A global variable is common to all workspaces (as long as each workspace declares the variable explicitly as global). global variables are extremely discouraged though. It is usually an indication of shoddy coding which in the long term will lead to extremely hard to locate bugs.
The standard way to share data between functions is to pass the return value(s) of one function as input argument(s) of the other.
2 Kommentare
Guillaume
am 8 Sep. 2015
It's unclear what difficutly you're encountering with passing values between functions. This is usually straightforward.
value = function1(input1, input2);
newvalue = function2(value);
or simply:
newvalue = function2(function1(input1, input2));
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!