Output data to new text file for every time step

조회 수: 3 (최근 30일)
Krish Desai
Krish Desai 2016년 3월 25일
답변: Vidya Viswanathan 2016년 3월 29일
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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by