Saving a text file as a time step array

1 vue (au cours des 30 derniers jours)
ben
ben le 9 Juil 2019
Commenté : KSSV le 10 Juil 2019
Hi. I am writing a simulation that analyzes the X,Y,Theta cordinates of the system. I am running the simulation by TMAX and for N particles. However, since TMAX is usually very large, I only want the data for every 100 time steps. Currently, my code gives me the information for every time step and particle position. However, it spits all the information onto one matrix, IE if I am running a 12 particle simulation for 50 steps (short for the example), the first 12 rows are particles 1-12 and there positions at time=10. However, the next 12 rows are particles 1-12 at time=20 and so on. How would I make each time step into its own text file? Below is my code:
fid = fopen('word.txt','w');
for nn = 1:TMAX
if mod(nn,10)==0
x = x + vel*cos(theta)*dt;
y = y + vel*sin(theta)*dt;
fprintf(fid, '%4.5f\t%4.5f\t%4.5f\n', x,y,theta);
end
end
Thank you very much!
  9 commentaires
ben
ben le 9 Juil 2019
Would posting the entire code be helpful? I know the loops could be better written. At the moment I am just looking to save the data that is generated by the particles though. Thank you for the help with the vel*co(theta)*dt though.
Guillaume
Guillaume le 9 Juil 2019
Would posting the entire code be helpful?
Probably, or a better explanation.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 9 Juil 2019
Modifié(e) : KSSV le 9 Juil 2019
YOu can save the required into a matrix using:
iwant = zeros([],3) ;
count = 0 ;
for nn = 1:TMAX
if mod(nn,10)==0
x = x + vel*cos(theta)*dt;
y = y + vel*sin(theta)*dt;
count = count+1 ;
iwant(count,:) = [x y theta] ;
end
end
  5 commentaires
ben
ben le 10 Juil 2019
Awesome-thank you very much for the help. It runs and does so smoother. thank you
KSSV
KSSV le 10 Juil 2019
Thanks is accepting and/or voting the answer. :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by