How to write the loop to collect data every step I want

I have M-file with dt = .1,dt= .01,dt=.001 I want to have the data is collected every 1 step for dt =.1, every 10 steps fro dt=.01, and 100 steps for dt=.001
the code start:
for i=-1:-1:-3
dt=10^i
step = 100
....... ..... the lot of code after this
....
I want to have all data to be collected at certain step size in the same M-file, in the code above it collects data every 100.
steps for all dt.
Thanks

3 Kommentare

I can run dt = .1 for step at 1 in one file, and then do the same with other one,but I need to graph all three data in the same graph . The dt is bigger the matrix is smaller , and the dt is smaller the matrix is bigger which is not the same dimension and it is not right to compare with other dt at that time.I need all of them in one file to see the different at certain time.
What do you mean by "data collected"? Do you have a data acquisition device that you're sampling from? Are you calculating data at every time step? Are you reading files that are being generated concurrently with the function running?
Please elaborate.
dt is time step , step is where I will collect the data ,
example i run some loop j= 0:dt:20 to calculate something, I will have a large number of data , So I want to collect the data at certain step not collecting the whole thing.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Dr. Seis
Dr. Seis am 9 Feb. 2012
You basically want to collect a sample at every 0.1 increment in time(?).
Setup a counter before your "for" loop and initialize to 0 i.e.,
counter = 0;
Then inside your for loop, where ever you perform computations for some period of time, you could add this:
for j = 1 : N
% your code for calculating value at each time step
if (mod(j-1,0.1/dt) == 0
counter = counter+1;
data(counter) = whatever_value_you_want;
end
end
Where "N" is the number of time steps. The above will save a value at the first time step and then every 0.1/dt time step(s) thereafter.

5 Kommentare

Due to precision issues, you may need to replace "0.1/dt" with something like "round(0.1/dt)" just to ensure you do get an integer there.
I did use this command but i dont know how the code will collect the data at certain step for different dt in the same file
like the loop with generate dt = .1, .01, .001 , how the data is going to collect at every 1 step, every 10 step , every 100 steps respectively.
Thats where the "mod" function comes in. If dt is 0.01, then mod(j-1,0.1/dt) = mod(j-1,10) and will be equal to zero when j = 1,11,21,... and so on. If dt is 0.001, then mod(j-1,0.1/dt) = mod(j-1,100) and will be equal to zero when j = 1,101,201,... and so on. So, when the mod function is equal to 0, you will collect/save your computed data value.
Also, I should clarify that the "for" loop I mentioned above is located inside the "for" loop you specified, which changes the value of "dt".
Thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by