Using the persistent function

조회 수: 28 (최근 30일)
Yewande Oni
Yewande Oni 2015년 9월 7일
편집: Stephen23 2015년 9월 8일
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
Stephen23
Stephen23 2015년 9월 8일
편집: Stephen23 2015년 9월 8일
Using globals is in second place on this list of bad programming practices:
And the documentation explains how to pass data between workspaces, note that the "Best Practice" is passing arguments:

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

채택된 답변

Guillaume
Guillaume 2015년 9월 7일
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
Yewande Oni
Yewande Oni 2015년 9월 8일
Ok, I did not really want to use the global or persistent variable but I was starting to feel stuck.
Could you advice me on how to pass the return value of one function and input it as an argument to another? - this is where I originally started but got lost and started to resort to using the persistent variable
Thanks
Guillaume
Guillaume 2015년 9월 8일
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));

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by