Constant number is changing value, why?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
So I have a 1 that should print out, but instead only on the first line does 1 print out, for the rest the number keeps changing. Why?
dt=5*10e-5;
timenumber=0;
for t=0:dt:2*dt %99 for 100 graphs
    figure;
    atom=1;
     for ind=1:1920
         U=(3*ind+linspace(1,3,3)-3);
         Phi= PHI_disp_RBME_ModePlot_N(U);
         r=xlocs_full(ind);
         omega=(f_disp_RBME(iMode,kPtMode)*2*pi);
         k=kappa_space(kPtMode);
         spatialphase=k*r;
         timephase=omega*t;
         displ=Phi*exp(i*((spatialphase)-(timephase)));
         xOvito(ind)=xOvito(ind)+magFact*displ(1);
         yOvito(ind)=yOvito(ind)+magFact*displ(2);
         zOvito(ind)=zOvito(ind)+magFact*displ(3);
         header0= 'Timestep:';
         header1= 'id';
         header2= 'type';
         header3='x';
         header4='y';
         header5='z';
         headernum='Number of atoms:';
         timenumber=(timenumber+1);
         if ind==1
             fileName=['dump',num2str(t*2000)];
         end;
         fid=fopen(fileName,'w');
         fprintf(fid, [ header0 '\n']);
         fprintf(fid, '%d \n', (timenumber/1920)-1');
         fprintf(fid, [headernum '\n']);
         fprintf(fid, '%d \n', 1920);
         fprintf(fid, [ header1 ' ' header2 ' ' header3 ' ' header4 ' ' header5 '\n']);
         fprintf(fid, '%f %f %f %f \n', [1 xOvito yOvito zOvito]');-------------->The one keeps changing
         fclose(fid);
     end
1 comentario
Respuestas (1)
  dpb
      
      
 el 25 de Mzo. de 2016
        
      Editada: dpb
      
      
 el 25 de Mzo. de 2016
  
      ...
for ind=1:1920
   ...
   xOvito(ind)=xOvito(ind)+magFact*displ(1);
   yOvito(ind)=yOvito(ind)+magFact*displ(2);
   zOvito(ind)=zOvito(ind)+magFact*displ(3);
   ...         header0= 'Timestep:';
   fprintf(fid, '%f %f %f %f \n', [1 xOvito yOvito zOvito]'); % The one keeps changing
   ...
Because as the above reduced code snippet shows, you're building arrays from 1:1920 for each of the three variables in the loop and the fprintf argument list uses the variable names without subscripting. So, each pass through the loop you'll get first one set of values, then two for each, then three, etc., ... Since the format is for four elements per output record, they'll wrap and the position of each relatively to each other will change each pass.
You undoubtedly intended to write
fprintf(fid, '%f %f %f %f \n', [1 xOvito(ind) yOvito(ind) zOvito(ind)]');
In summary, the '1' didn't change its value, it simply gets placed in a different location owing to the number of values being printed each pass through the loop.
ADDENDUM
While not related to the issue, you'll save quite a lot if you preallocate the three arrays before beginning the loop over ind --
...
xOvito=zeros(1920,1);  % preallocate 
yOvito=zeros(1920,1);  % preallocate 
zOvito=zeros(1920,1);  % preallocate 
for ind=1:1920
 ...
 xOvito(ind)=xOvito(ind)+magFact*displ(1);
 yOvito(ind)=yOvito(ind)+magFact*displ(2);
 zOvito(ind)=zOvito(ind)+magFact*displ(3);
 ...
0 comentarios
Ver también
Categorías
				Más información sobre Performance and Memory en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


