Global variables overwritten as empty matrices
Show older comments
Hi all,
I've read a few posts about the global variables but I'm still struggling. I have a main script which calls in both other scripts and other functions. I need some variables to be accessible by all the scripts and functions called. I tried the persistent command but this wouldn't apply to the scripts I think. Instead I've been using the global command. I first define my variable 'subCode' as global at the beginning of the main script, subsequently entering in a value for subCode. The first function that is called accesses this information correctly, and in this function I again use 'global subCode' at the very beginning of the function. However, in a subsequent function where I follow the same pattern (setting 'global subCode' at the very beginning of the function) it returns an error and when I look at the workspace it is an empty matrix. I'm not super proficient in matlab and I'm struggling to find an answer, so any (kind) guidance would be very much appreciated!
1 Comment
Stephen23
on 27 Aug 2018
"I've read a few posts about the global variables..."
"... and I'm struggling to find an answer"
Avoid using global variables. They are more trouble than they are worth:
Just pass your data properly as input/output arguments, or use nested functions:
Answers (0)
Categories
Find more on Whos 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!