如何在matlab中打印每轮后的变量值?

如何在matlab中打印每轮后的变量值?,matlab,for-loop,Matlab,For Loop,我有100个节点使用以下for循环: xm1=100; %diameters of sensor network ym1=100; sink1.x=50; %distance of base station from the network sink1.y=120; nl = 100; %no of nodes pl=0.1; %probibilty of a node to become cluster head El1= 0; Eint=0

我有100个节点使用以下for循环:

xm1=100;      %diameters of sensor network
ym1=100;

sink1.x=50;  %distance of base station from the network
sink1.y=120;

nl = 100;         %no of nodes

pl=0.1;          %probibilty of a node to become cluster head
El1= 0;
Eint=0.5;          %energy supplied to each node 
Etl =0 ;
for i=1:1:nl
    Sl(i).xd=rand(1,1)*xm1;         %it will distribute the nodes in 1 dimension in x axis randomly.
    XR(i)=Sl(i).xd;                 %we store its value in xr
    Sl(i).yd=rand(1,1)*ym1;           %it will distribute the nodes in 1 dimension in y axis randomly
    YR(i)=Sl(i).yd;
    Sl(i).id = i;
    Sl(i).G=0;                        % as the no of node that have been cluster head is zero 0
    Sl(i).E=Eint; %%*(1+rand*a);     
    %fprintf(' Sensor node energy:  %15f \n',Eint);   %?
    %initially there are no cluster heads only nodes
    Sl(i).type='N';
end
团簇的形成类似tis

for r=1:1:rmax
     for i=1:1:nl
            if(Sl(i).E>0)
                temp_randl=rand;
                if ( (Sl(i).G)<=0)

                    if(temp_randl<= (pl/(1-pl*mod(r,round(1/pl)))))
                        countCHsl=countCHsl+1;
                        packets_TO_BSl=packets_TO_BSl+1;
                        PACKETS_TO_BSl(r+1)=packets_TO_BSl;
                        Sl(i).type='C';
                        Sl(i).G=round(1/pl)-1;
                        Cl(clusterl).xd=Sl(i).xd;
                        Cl(clusterl).yd=Sl(i).yd;
                        distancel=sqrt( (Sl(i).xd-(Sl(n+1).xd) )^2 + (Sl(i).yd-(Sl(n+1).yd) )^2 );
                        Cl(clusterl).distance=distancel;
                        Cl(clusterl).id=i;
                        X(clusterl)=Sl(i).xd;
                        Y(clusterl)=Sl(i).yd;
                        clusterl=clusterl+1;
                        distancel;
                        if (distancel>do)
                            Sl(i).E=Sl(i).E- ( (ETX+EDA)*(4000) + Emp*4000*(distancel*distancel*distancel*distancel ));
                        end
                        if (distancel<=do)
                            Sl(i).E=Sl(i).E- ( (ETX+EDA)*(4000)  + Efs*4000*(distancel * distancel ));
                        end
                    end

                end
                % S(i).G=S(i).G-1;

            end
        end
        STATISTICS.COUNTCHS(r+1)=countCHsl;

        for i=1:1:nl
            if ( Sl(i).type=='N' && Sl(i).E>0 )
                if(clusterl-1>=1)
                    min_disl=Inf;
                    min_dis_clusterl=0;
                    for cl=1:1:clusterl-1
                        templ=min(min_disl,sqrt( (Sl(i).xd-Cl(cl).xd)^2 + (Sl(i).yd-Cl(cl).yd)^2 ) );
                        if ( templ<min_disl )
                            min_disl=templ;
                            min_dis_clusterl=cl;
                        end
                    end


                    min_disl;
                    if (min_disl>do)
                        Sl(i).E=Sl(i).E- ( ETX*(4000) + Emp*4000*( min_disl *min_disl * min_disl * min_disl));
                    end
                    if (min_disl<=do)
                        Sl(i).E=Sl(i).E- ( ETX*(4000) + Efs*4000*( min_disl * min_disl));
                    end
                    Sl(Cl(min_dis_clusterl).id).E =Sl(Cl(min_dis_clusterl).id).E- ( (ERX + EDA)*4000 );
                    packets_TO_CHl=packets_TO_CHl+1;

                    Sl(i).min_disl=min_disl;
                    Sl(i).min_dis_clusterl=min_dis_clusterl;
                else
                    min_disl=sqrt( (Sl(i).xd-Sl(n+1).xd)^2 + (Sl(i).yd-Sl(n+1).yd)^2 );
                    if (min_disl>do)
                        Sl(i).E=Sl(i).E- ( ETX*(4000) + Emp*4000*( min_dis1 *min_dis1 * min_dis1 * min_dis1));
                    end
                    if (min_disl<=do)
                        Sl(i).E=Sl(i).E- ( ETX*(4000) + Efs*4000*( min_disl * min_disl));
                    end
                    packets_TO_BSl=packets_TO_BSl+1;
                end
            end
        end

end
简单的 S1(i.E.), 应该行。

我假设r代表圆形,所以您可以在最后一个端点之前添加以下任何一行来打印剩余能量:

  • sprintf('%0.2f\n',Sl(:).E)
  • disp([Sl(:).E]')
  • Sl(:).E

更多的细节可能会有所帮助。我的意思是,如果在任何变量后加逗号,其值将被打印出来感谢您的回答。我必须在每一轮中找到每个节点的能量。让我有三个节点。每个节点的初始能量为1焦耳。10轮后,节点1的初始能量为0.6,节点2为0.5,节点3为0.7。如何打印这个?当我在程序最后一次结束之前使用sprintf(“%0.2f”,S1(I).E)时。当我使用disp(S1(I).E)时,它给出了1.42。它是每轮打印第100个节点的值。如何打印一轮中所有节点的值。还有如何将这些结果保存到文件中。我已经更新了答案,以打印每轮的所有值。另一个问题是保存到文件。这将取决于您想要的文件格式。但是您可以尝试:fid=fopen('myfile.txt');fprintf('%0.2f\n',Sl(:).E);fclose(fid);对于txt文件。如果我回答了你的问题,请考虑接受,这样别人会从中受益,如果他们面临类似的问题。非常感谢。是的,现在使用以下行fid1=fopen('energy_every_round.txt','w')在文件上打印每个节点的能量;fprintf(fid1,“%s\t%s\t\n”,“NUM”,“energy”);fprintf(fid1,'%0.2f\n',Sl(:).E);fclose(fid);但文件中只打印能量值。我还想看看节点id。有没有办法做到这一点?i、 e.在文件上打印节点ID和节点能量。请提供答案。我会立即将其标记为已接受。这只是给出第100个节点的能量。1.1.1.1.1.1.1.7 7 7 7 7 7 7 7 7个E-02,0.0 0 1.116808080 0 0 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 E-02,0.07 3.589620e-02,0.11 1.164451e-01,0.08 7.880229e-02,0.16 1.275763e-01,0.05 3.319550e-02,0.05 1.663428e-02,0.00 2.107233e-02,0.08 1.110767e-01,0.07 8.944071e-02,0.01 7.116244e-02,
fprintf('%0.2f \n',Sl(:).E);