Reference to non-existent field 'xd'. Kindly help .
2 views (last 30 days)
Show older comments
I am working on a network protocol & I seem to be getting stuck at a point with the error "Reference to non-existent field 'xd'.".
the code I'm working on is as follows : -
clc
clear all;
close all;
%message size in bits
l=2000;
%field dimensions
xm=300;
ym=300;
%sink = center of the field
sink.x=0.5*xm;
sink.y=0.5*ym;
%number of sensor nodes
n=250;
%probability of a node to be elected cluster head
p=0.1;
%initial energy
Eo=0.5;
ETX=50*0.000000001;
ERX=50*0.000000001;
Eelec=ETX;
%transmit amplifier types
Efs=10e-12;
Emp=0.0013e-12;
%data aggregation energy
EDA=5*0.000000001;
%the maximum number of rounds
rmax=250;
do=sqrt(Efs/Emp);
%optimum number of cluster heads
k=20;
t=0;
Et=0;
for h=1:1
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
Et=0;
for i=1:1:n
S(i).xd=rand(1,1)*xm;
XR(i)=S(i).xd;
S(i).yd=rand(1,1)*ym;
YR(i)=S(i).yd;
%distance between each sensor node & the cluster head
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
S(i).distance=distance;
S(i).G=0;
%initially there are no cluster heads only nodes
S(i).type='N';
S(i).E=Eo;
Et=Et+S(i).E;
figure(h*10)
plot(S(i).xd,S(i).yd,'bo');
text(S(i).xd+1,S(i).yd-0.5,num2str(i));
hold on;
end
%plotting the cluster head
plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
text(S(n+1).xd+1,S(n+1).yd-0.5,num2str(n+1));
hold off ;
countCHs=0; %variable, counts the cluster head
cluster=1; %cluster is initialized as 1
flag_first_dead=0; %flag tells the first node dead
flag_half_dead=0; %flag tells the 10th node dead
flag_all_dead=0; %flag tells all nodes dead
first_dead=0;
half_dead=0;
all_dead=0;
allive=n;
%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
packets_TO_BS_per_round=0;
for r=0:1:rmax
r
packets_TO_BS_per_round=0;
%Operations for epochs
if(mod(r, round(1/p) )==0)
for i=1:1:n
%every node is initialized to not belonging to a cluster and
%having 0 G value in the beginning
S(i).G=0;
S(i).cl=0;
end
end
%hold off;
%Number of dead nodes
dead=0;
for i=1:1:n
%counting the number of nodes with non zero G value
if (S(i).G > 0)
t = t + 1;
end
%checking if there is a dead node
if (S(i).E<=0)
%plotting dead nodes
plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
if (dead==1)
if(flag_first_dead==0)
%first dead node useful in calculating the network
%lifetime
first_dead=r;
flag_first_dead=1;
end
end
if(dead==0.5*n)
if(flag_half_dead==0)
half_dead=r;
flag_half_dead=1;
end
end
if(dead==n)
if(flag_all_dead==0)
all_dead=r;
flag_all_dead=1;
end
end
%hold on;
end
if S(i).E>0
S(i).type='N';
end
end
%plotting the sink/base station
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS.DEAD(h,r+1)=dead;
STATISTICS.ALLIVE(h,r+1)=allive-dead;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
countCHs=0;
cluster=1;
for i=1:1:n
%Cluster Head election process if the node has energy/normal
%node/alive node
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
%new Cluster Head will be elected when G value falls below0
if ( t < k)
if ( t > 0)
while (countCHs < t)
countCHs = countCHs + 1;
packets_TO_BS=packets_TO_BS+1;
packets_TO_BS_per_round=packets_TO_BS_per_round+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';%CH election
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'dr');
end
if(t==0 & S(i).type~='C')
S(i).G = round(1/p)-1;
end
%electing k-t cluster heads
while(countCHs<k-t)
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
packets_TO_BS_per_round=packets_TO_BS_per_round+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'dr');
end
end
else
while(countCHs<t)
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p)-1;
plot(S(i).xd,S(i).yd,'dr');%plots cluster heads with red diamonds
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'k*');
d=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=d;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
end
end
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
%Calculation of Energy dissipated
distance;
if (distance>=do)
S(i).E=S(i).E- ( (Eelec)*(l) + Emp*l*( distance*distance*distance*distance ));
end
if (distance<do)
S(i).E=S(i).E- ( (Eelec)*(4000) + Efs*l*( distance * distance ));
end
end
end
end
end
STATISTICS.COUNTCHS(h,r+1)=countCHs;
% or STATISTICS.COUNTCHS(h,r+1)=clster-1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Election of Associated Cluster Head for Normal Nodes
for i=1:1:n
if ( S(i).type=='N' && S(i).E>0 )
if(cluster-1>=1)
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
min_dis_cluster=0;
for c=1:1:cluster-1
temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
if ( temp<min_dis )
min_dis=temp;
min_dis_cluster=c;
end
end
if(min_dis_cluster~=0)
min_dis;
if (min_dis>=do)
S(i).E=S(i).E- ( Eelec*(l) + Emp*l*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<do)
S(i).E=S(i).E- ( Eelec*(l) + Efs*l*( min_dis * min_dis));
end
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*l );
packets_TO_CH=packets_TO_CH+1;
else
min_dis;
if (min_dis>=do)
S(i).E=S(i).E- ( ETX*(l) + Emp*l*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<do)
S(i).E=S(i).E- ( ETX*(l) + Efs*l*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
packets_TO_BS_per_round=packets_TO_BS_per_round+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
else
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
if (min_dis>=do)
S(i).E=S(i).E- ( Eelec*(l) + Emp*l*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<do)
S(i).E=S(i).E- ( Eelec*(l) + Efs*l*( min_dis * min_dis));
end
packets_TO_BS=packets_TO_BS+1;
packets_TO_BS_per_round=packets_TO_BS_per_round+1;
end
end
end
STATISTICS.PACKETS_TO_CH(h,r+1)=packets_TO_CH;
STATISTICS.PACKETS_TO_BS(h,r+1)=packets_TO_BS;
STATISTICS.PACKETS_TO_BS_PER_ROUND(h,r+1)=packets_TO_BS_per_round;
STATISTICS.THROUGHPUT(h,r+1)=STATISTICS.PACKETS_TO_BS(h,r+1)+STATISTICS.PACKETS_TO_CH(h,r+1);
En=0;
for i=1:n
if S(i).E<=0
continue;
end
En=En+S(i).E;
end
ENERGY(r+1)=En;
STATISTICS.ENERGY(h,r+1)=En;
end
first_dead_LEACH(h)=first_dead
half_dead_LEACH(h)=half_dead
all_dead_LEACH(h)=all_dead
for r=0:rmax
STATISTICS.DEAD(h+1,r+1)=sum(STATISTICS.DEAD(:,r+1))/h;
STATISTICS.ALLIVE(h+1,r+1)=sum(STATISTICS.ALLIVE(:,r+1))/h;
STATISTICS.PACKETS_TO_CH(h+1,r+1)=sum(STATISTICS.PACKETS_TO_CH(:,r+1))/h;
STATISTICS.PACKETS_TO_BS(h+1,r+1)=sum(STATISTICS.PACKETS_TO_BS(:,r+1))/h;
STATISTICS.PACKETS_TO_BS_PER_ROUND(h+1,r+1)=sum(STATISTICS.PACKETS_TO_BS_PER_ROUND(:,r+1))/h;
STATISTICS.THROUGHPUT(h+1,r+1)=sum(STATISTICS.THROUGHPUT(:,r+1))/h;
STATISTICS.COUNTCHS(h+1,r+1)=sum(STATISTICS.COUNTCHS(:,r+1))/h;
STATISTICS.ENERGY(h+1,r+1)=sum(STATISTICS.ENERGY(:,r+1))/h;
end
first_dead=sum(first_dead_LEACH)/h;
half_dead=sum(half_dead_LEACH)/h;
all_dead=sum(all_dead_LEACH)/h;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=0:rmax;
figure(1)
plot(r,STATISTICS.DEAD(h+1,r+1));
title('dead nodes')
figure(2)
plot(r,STATISTICS.ALLIVE(h+1,r+1));
title('live nodes')
figure(3)
plot(r,STATISTICS.PACKETS_TO_BS(h+1,r+1));
title('pkts to BS')
figure(4)
plot(r,STATISTICS.PACKETS_TO_BS_PER_ROUND(h+1,r+1));
title('pkts to BS per round')
figure(5)
plot(r,STATISTICS.PACKETS_TO_CH(h+1,r+1));
title('pkts to CH')
figure(6)
plot(r,STATISTICS.THROUGHPUT(h+1,r+1));
title('THROUGHPUT')
figure(7)
plot(r,STATISTICS.COUNTCHS(h+1,r+1));
title('COUNTCHS')
figure(8)
plot(r,STATISTICS.ENERGY(h+1,r+1));
title('Average Residual Energy')
Now I am getting the error in line 216, temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
Please note that this isn't the first time I am using S(i).xd, I have used it before also.
I am stumped. Please help. Just guide me towards the correct syntax/usage.
I am out of touch with matlab.
0 Comments
Accepted Answer
Stephen23
on 19 Nov 2018
Edited: Stephen23
on 19 Nov 2018
The problem is related to structure C, not structure S.
The fields xd and yd of structure C are defined inside while loops whose conditions are never true on the 250th iteration of the r loop, thus you do not define those fields. This pretty easy to check: just run the code until the error occurs, then check the values in the workspace:
>> h
h = 1
>> i
i = 1
>> r
r = 250
>> C
C =
1x250 struct array containing the fields:
distance
id
>> countCHs
countCHs = 0
>> t
t = 0
>> k
k = 20
It is quite clear that the conditions, e.g.:
while (countCHs < t)
are not met, so those while loops don't run.
0 Comments
More Answers (1)
Alan Weiss
on 19 Nov 2018
The problem isn't S(i).xd, it is C(c).xd that doesn't exist. Your structure C(c) has fields distance and id but not xd.
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!