Using a print statement in a loop

11 views (last 30 days)
Luke
Luke on 18 Oct 2014
Commented: Image Analyst on 29 Jul 2020
Hey guys, I'm having some trouble with running an fprintf statement in a loop. What I did is created a character array, and I want to run a loop to print the values of another matrix with the labels from that character array. Here's my code:
x = (pi/4);
M = [-1 0 0 0 0 0 0 0;
0 0 0 0 -1 0 0 0;
1 0 cos(x) 0 0 0 0 0;
0 -1 -sin(x) 0 0 0 0 0;
0 0 0 1 0 0 0 0;
0 1 0 0 0 1 0 0;
0 0 -sin(x) -1 0 0 0 1;
0 0 cos(x) 0 1 0 1 0];
E = [1000 0 0 500 -500 0 0 0]';
f1 = M\E;
F = char(['F1'; 'F2'; 'F3'; 'F4'; 'F5'; 'R1'; 'R2'; 'R3']);
for x = x
fprintf('%.3f = %.3f Newtons \n\n', F, f1)
end
My result is something really strange, and I don't understand why it is what it is:
70.000 = 70.000 Newtons
70.000 = 70.000 Newtons
70.000 = 82.000 Newtons
82.000 = 82.000 Newtons
49.000 = 50.000 Newtons
51.000 = 52.000 Newtons
53.000 = 49.000 Newtons
50.000 = 51.000 Newtons
-1000.000 = -1500.000 Newtons
1414.214 = -500.000 Newtons
0.000 = 1500.000 Newtons
-1000.000 = 500.000 Newtons
What I WANT to do is have each label of F pair up with its respective value from f1. I have NO idea why it's printing numerical values for what should be a character string.
Anyway, thanks in advance!

Answers (2)

Image Analyst
Image Analyst on 18 Oct 2014
F is a string, so use %s instead of %f:
fprintf('%s = %.3f Newtons \n\n', F, f1)
  2 Comments
Luke
Luke on 18 Oct 2014
Wow, I can't believe I forgot that. However, when I did try that just now, it gave me a slew of nonsensical stuff:
FFFFFRRR12345123 = -1000.000 Newtons
-1.500000e+03 = 1414.214 Newtons
= 0.000 Newtons
1.500000e+03 = -1000.000 Newtons
5.000000e+02 = >>
Image Analyst
Image Analyst on 18 Oct 2014
Your loop iterator, x, was wrong, and you need to index F and f1. Try this:
x = (pi/4);
M = [-1 0 0 0 0 0 0 0;
0 0 0 0 -1 0 0 0;
1 0 cos(x) 0 0 0 0 0;
0 -1 -sin(x) 0 0 0 0 0;
0 0 0 1 0 0 0 0;
0 1 0 0 0 1 0 0;
0 0 -sin(x) -1 0 0 0 1;
0 0 cos(x) 0 1 0 1 0]
E = [1000 0 0 500 -500 0 0 0]'
f1 = M\E
F = char(['F1'; 'F2'; 'F3'; 'F4'; 'F5'; 'R1'; 'R2'; 'R3'])
for row = 1:length(f1)
fprintf('%s = %.3f Newtons \n\n', F(row,:), f1(row))
end

Sign in to comment.


Roja G
Roja G on 29 Jul 2020
%%%LEACH%%
clear ;
%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%
% Field Dimensions - x and y maximum (in meters)
xm=100;
ym=100;
% x and y coordinates of the sink
% placing the sink at centre of the field
sink.x=0.5*xm;
sink.y=0.5*ym;
% Number of nodes in the field
n=100;
% Optimal election probability of a node to be come cluster head
p=0.1;
% Energy model (ALL VALUES IN JOULES)
% Intial Energy(same for all nodes - homogeneous scenario)
Eo=0.5;
% Eelec=Etx=Erc
ETX=50*0.000000001; % transmission power
ERX=50*0.000000001;
% Transmit amplifier types
Efs=10*0.00000000001;
Emp=0.0013*0.00000000001;
% Data aggreagation energy
EDA=5*0.000000001;
% Values for Hetereogeneity
% Percentage of nodes than are advanced
m=0.1;
% \alpha
a=1;
% maximum number of rounds
rmax=4000;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%END OF PARAMETERS%%%%%%%%%%%%%%%%%
% Computaion of do
do=sqrt(Efs/Emp); % approx. value of do = 84.73
% Creation of the random sensor network
figure(1);
for i=1:1:n
% whenever we perform node deployment, we need to create a data type for sensor node in Matlab, since a node can have many attributes
% here S is the data type of our sensor node
S(i).xd=rand(1,1)*xm; % xd represents x-coordinate of node generated randomly
XR(i)=S(i).xd; % sequential indexing of node
S(i).yd=rand(1,1)*ym; % yd represents y-coordinate of node generated randomly
YR(i)=S(i).yd; % sequential indexing of node
S(i).G=0; % shows whether the node belongs to any group or not, here it doesnt belong to any group so 0
%intially there are no cluster heads only nodes
S(i).type='N'; % states that it is just a node, not a cluster head
temp_rnd0=i;
%Random election of normal nodes
if(temp_rnd0>=m*n+1)
S(i).E=Eo; % initial energy of the node is Eo
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on; % cause we are plotting multiple nodes in a single figure
end
%Random election of advanced nodes
if(temp_rnd0<m*n+1)
S(i).E=Eo;
S(i).ENERGY=1;
plot(S(i).xd,S(i).yd,'+');
hold on;
end
end
S(n+1).xd=sink.x; % x-coordinate of sink
S(n+1).yd=sink.y; % y-coordinate of sink
plot(S(n+1).xd,S(n+1).yd,'x');
%First Iteration
figure(1);
%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=0;
cluster=1; % initializing clusters
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
for r=0:1:rmax % max number of rounds
r;
% Operation for epoch i.e., how the cluster head is getting selected
% after initializing epoch, the selection procedure starts
if(mod(r, round(1/p) )==0)
for i=1:1:n
S(i).G=0;
S(i).c1=0;
end
end
hold off;
% Number of dead nodes
dead=0;
% Number of dead Advanced Nodes
dead_a=0;
% Number of dead Normal Nodes
dead_n=0;
% counter for bit transmitted to base station and to cluster head
packet_TO_BS=0;
packet_TO_CH=0;
% counter for bit transmitted to base station and to cluster head per round
PACKET_TO_CH(r+1)=0;
PACKET_TO_BS(r+1)=0;
figure(1);
for i=1:1:n
% checking if there is a dead node
if(S(i).E<=0)
plot(S(i).xd,S(i).yd,'red .');
%plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
if(S(i).ENERGY==1)
dead_a=dead_a+1;
end
if(S(i).ENERGY==0)
dead_n=dead_n+1;
end
hold on;
end
if S(i).E>0
S(i).type='N';
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
end
hold on;
end
totalenergy(i)=S(i).E;
% if r+1==1095
% S(i).E=0;
% end
end
total_energy=sum(totalenergy);
plot(S(n+1).xd,S(n+1).yd,'+');
STATISTICS(r+1).DEAD = dead;
DEAD(r+1) = dead;
if r+1==1095
S(i).E=0;
end
% When the first node dies
if(dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
% Election of cluster heads
if(temp_rand<=(p/(1-p*mod(r,round(1/p))))*(S(i).E/total_energy)) % *(S(i).E/total_energy) is the modified leach formula, which represents residual energy of the node by the total energy of the network
countCHs=countCHs+1;
PACKET_TO_BS(r+1)=packet_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,'k*');
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2+ (S(i).yd-(S(n+1).yd) )^2 ); % distance formula
% corresponding distance from base station follows 2 types of routing based on whether d is < or > do (declared at start of the code)
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) % multipath routing, current energy of the node = S(i).E, energy dissipation = (ETX+EDA)*(4000) + Emp*4000*(distance*distance*distance*distance)
S(i).E = S(i).E - ((ETX+EDA)*(4000) +Emp*4000*(distance*distance*distance*distance)); % packet size = 4000, Emp - multipath routing coefficient
end
if(distance<=do) % free space routing, energy dissipation = (ETX+EDA)*(4000) +Efs*4000*(distance*distance)
S(i).E = S(i).E - ((ETX+EDA)*(4000) +Efs*4000*(distance*distance)); % free space routing coefficient
end
end
end
end
end
STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;
% Election of associated cluster head for normal nodes
% calculating distance from cluster head to member nodes, if nearby then cluster member
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=1;
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
% Energy dissipated by associated cluster head (same as above)
min_dis;
if (min_dis>do)
S(i).E=S(i).E- (ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- (ETX*(4000) + Efs*4000*( min_dis * min_dis)); % energy dissipated while receiving the information from the nodes
end
%Energy dissipated
if(min_dis>0)
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( (ERX+EDA)*4000); % ERX+EDA)*4000 - energy dissipated by receiving power of each and every cluster head
PACKETS_TO_CH(r+1)=n-dead-cluster+1;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
end
end
end
hold on;
countCHs;
rcountCHs=rcountCHs+countCHs;
end
AlliveLeach = 100 - DEAD; % after total energy is dissipated, nodes start becoming dead one after the other
% this is the criterion (performance of alive nodes) for analyzing the network lifetime
%plot (1:4001,AlliveLeach,'-r') % can be upto 40001 rounds
  2 Comments
Roja G
Roja G on 29 Jul 2020
could you please help me.. i want to print a statement to find the cluster head formation and residual energy for each round . how to use the for loop to the above code
Image Analyst
Image Analyst on 29 Jul 2020
This is not an answer to Luke's question.
Please start your own question after you read this link.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!