Saving a text file as a time step array

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 件のコメント

KSSV
KSSV 2019 年 7 月 9 日
YOu want to write a seperate file for each step?
ben
ben 2019 年 7 月 9 日
Yes, would that be possible?
Guillaume
Guillaume 2019 年 7 月 9 日
編集済み: Guillaume 2019 年 7 月 9 日
Anything is possible (with different amount of difficulty of course).
It's not clear how the snippet of code you show is relevant to your question. What you show will print TMAX/10 times the same thing, while wasting a lot of time doing iterations that don't print or calculate anything.
edit: I missed that x and y are a cumulative sum, so you are printing different things each time. However, you still waste a lot of time counting and not doing anything.
ben
ben 2019 年 7 月 9 日
So then how could I re-write the code to print each time step in an individual file? Currently it gives me all the variables in a singular matrix of all of the time steps. I don't have much experiance with this and was just confused on how to write it.
Guillaume
Guillaume 2019 年 7 月 9 日
編集済み: Guillaume 2019 年 7 月 9 日
You haven't given us enough information for us to answer. Are the variables, x, y, vel, dt, theta scalars? Are any of them matrices? Where do the particles you mention figure into any of this.
Are the equations you show the actual equations. If so, why are you using a loop?
ben
ben 2019 年 7 月 9 日
I'm very sorry about that. The variables are X,Y, and theta. The equations are part of a greater set of the simulation. Nothing is a matrix. Basically I have a simulation that maps how different particles are moving and am looking to get the particles X Y and theta cordinates on the graph every 100 steps in the simulation. While these equations are the general map for the simulation to follow, I am only looking to print X Y and theta into a matrix/chart, where the rows are the particle numbers and the columns are the various variables(x,y,theta). However, I would like each timestep (100,200,300...TMAX) to be its own matrix/chart. How would I go about doing that? Thank you very much
Guillaume
Guillaume 2019 年 7 月 9 日
I understand what you want to achieve. What I don't understand is what you start from. The code you show just add a constant (vel*cos(theta)*dt) each step of the loop. This could be rewritten as simply:
x0 = ???
x = (1:TMAX) .* vel*cos(theta)*dt + x0; %calculate x for 1*constant, 2*constant, etc.
which can then be easily sliced any way you want. Again, I don't see how the particles figure into this, so I'm not sure if that's what you're after.
ben
ben 2019 年 7 月 9 日
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 2019 年 7 月 9 日
Would posting the entire code be helpful?
Probably, or a better explanation.

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2019 年 7 月 9 日
編集済み: KSSV 2019 年 7 月 9 日

1 投票

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 件のコメント

ben
ben 2019 年 7 月 9 日
Thank you very much! However, it is saying there is an error on line two (count=count+1 ;). Forgive me but I am unsure what the error is..
KSSV
KSSV 2019 年 7 月 9 日
There was typo error...I have edited it...try now.
Guillaume
Guillaume 2019 年 7 月 9 日
for nn = 1:TMAX
if ~mod(nn, 10)
%do something
end
end
is a complete waste of time. You're wasting 90% of the iterations not doing anything useful.
for nn = 1:TMAX/10
%do something
end
would achieve exactly the same result.
Of course, as I've already pointed out, the loop is not even needed.
ben
ben 2019 年 7 月 10 日
Awesome-thank you very much for the help. It runs and does so smoother. thank you
KSSV
KSSV 2019 年 7 月 10 日
Thanks is accepting and/or voting the answer. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

ben
2019 年 7 月 9 日

コメント済み:

2019 年 7 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by