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
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?
Baki
Baki 2014 年 11 月 25 日
編集済み: Baki 2014 年 11 月 25 日
The idea is already to update Nodes. If node string is not inside Nodes structure, add node to Nodes. I want Nodes to be structure or cell-array (whichever works)

サインインしてコメントする。

回答 (1 件)

Baki
Baki 2014 年 11 月 25 日

0 投票

Problem solved. I added the following lines to the main code:
global Nodes nindex
Nodes='';
nindex=0;

カテゴリ

ヘルプ センター および File ExchangeInteractive Control and Callbacks についてさらに検索

質問済み:

2014 年 11 月 25 日

回答済み:

2014 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by