Global Variable calling error
古いコメントを表示
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 件のコメント
Adam
2014 年 11 月 25 日
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?
回答 (1 件)
カテゴリ
ヘルプ センター および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!