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 Commenti

Adam
Adam il 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?
Baki
Baki il 25 Nov 2014
Modificato: Baki il 25 Nov 2014
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)

Accedi per commentare.

Risposte (1)

Baki
Baki il 25 Nov 2014
Problem solved. I added the following lines to the main code:
global Nodes nindex
Nodes='';
nindex=0;

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Richiesto:

il 25 Nov 2014

Risposto:

il 25 Nov 2014

Community Treasure Hunt

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

Start Hunting!

Translated by