Global Variable calling error
1 view (last 30 days)
Show older comments
Hi, I have defined a function with global variables. I get the following message: "The left hand side is initialized and has an empty range of indices. However, the right hand side returned one or more results." In the main code, node is of char class, nindex is 0, Nodes=''
The function code:
function nodenumber=nodechecker(node)
global Nodes nindex
v=strcmp(Nodes,node);
indis=find(v,1);
if isempty(indis)==1
nindex=nindex+1;
Nodes{nindex}=node;
nodenumber=nindex;
else
nodenumber=indis;
end
2 Comments
Adam
on 25 Nov 2014
This is one of the big flaws of using global variables. In a good design this function should be able to know all it needs to know about the variables that are within its workspace so it can validate them and know what to expect of them.
Having a global variable drop in that was defined somewhere else, can be changed anywhere else and at any time is not good practice.
Is there a reason why you can't just pass in Nodes and nindex as arguments to your function so that they have function-bounded scope?
Answers (1)
See Also
Categories
Find more on Logical 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!