Output data to new text file for every time step

5 次查看(过去 30 天)
where should I put my printf's that for the current time steps that I have, I get 5 new files for each step with the x,y,z positions contained in them?
dt=5*10e-5;
for t=0:dt:4*dt %99 for 100 graphs
figure;
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);
end
header1= 'id';
header2= 'type';
header3='x';
header4='y';
header5='z';
fid=fopen('Dumped.txt','w');
%fprintf(fid, [header 1 ' ' header 2 ' ' header 3 ' ' header 4 ' ' header 5 ' '\n']);
fprintf(fid, [ header1 ' ' header2 ' ' header3 ' ' header4 ' ' header5 '\n']);
fprintf(fid, '%f %f %f \n', [xOvito yOvito zOvito]');
fclose(fid);
plot3(xOvito,yOvito,zOvito,'ko','markerfacecolor','r', 'markersize', 7);
xrotation = 0;
yrotation = 0;
view(xrotation, yrotation);
xlabel('x')
ylabel('y')
zlabel('z')
axis equal
xview=0;
yview=0;
view(xview,yview);
end
After looking at my code, I see that each time I rewrite the text file, so I only have the last time step's data. How do I change this?
  3 个评论
Walter Roberson
Walter Roberson 2016-3-25
You do not initialize or change i, so it is going to have its default value as sqrt(-1)
Krish Desai
Krish Desai 2016-3-25
What should I change i to? What is supposed to be there?

请先登录,再进行评论。

回答(1 个)

Vidya Viswanathan
Vidya Viswanathan 2016-3-29
Hi Krish,
I believe you are looking for a method to append the data from the next time stamp to the file rather than rewriting it. The function "fopen" provides you with an option to open the file with the appropriate access. To append to the existing file, you would have to open it as follows:
>> fid=fopen(filename,'a');
You could refer to the following documentation page for the same information:
I hope this answers your question.
Regards,
Vidya

Community Treasure Hunt

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

Start Hunting!

Translated by