Output data to new text file for every time step

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 commentaires

Krish Desai
Krish Desai le 25 Mar 2016
Modifié(e) : Krish Desai le 25 Mar 2016
Tried to use sprintf, only created one file....
%TimeStep
%kPtMode=3;
%iMode=6;
dt=5*10e-5;
steps=4;
for t=0:dt:steps*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
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);
header1= 'id';
header2= 'type';
header3='x';
header4='y';
header5='z';
filename=sprintf('%s_%d','filename',i);
fid=fopen(filename,'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);
end
end
You do not initialize or change i, so it is going to have its default value as sqrt(-1)
What should I change i to? What is supposed to be there?

Connectez-vous pour commenter.

Réponses (1)

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