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 个评论

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 个)

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

类别

帮助中心File Exchange 中查找有关 Interactive 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