MATLAB Answers

0

How to make a "local global " variable?

Asked by wei zhang on 31 May 2019
Latest activity Commented on by wei zhang on 1 Jun 2019
Hello,
I would like to share some variables among some functions. It is inconvenience to make them all as arguments, because that makes the command too long. Is there a way to add these variable in a special "global variable pool"? A global variable is defined under all functions, which is not appropriate for me. I only want them to be shared among several functions.
Thank you for any helpful suggestion.

  3 Comments

"It is inconvenience to make them all as arguments, because that makes the command too long"
The standard efficient solution is to put them into one structure and pass that.
Rik
on 31 May 2019
@Stephen since using a struct is likely to be the best solution by far, I would suggest you move it to the answers section.
@Stephen @Rik
Yes. You are right. Thank you for your good solution.

Sign in to comment.

Products

1 Answer

Answer by Walter Roberson
on 31 May 2019
 Accepted Answer

No, that does not exist.
  • you can create accessor functions that deal with persistent variables inside a private directory
  • you can put the functions togther in a class with class variables
  • if all relevant functions can be in the same source file, then you can use nested functions with shared variable
  • or pass a struct instead

  0 Comments

Sign in to comment.