How to work out time from defined time steps in code on plot??

2 次查看(过去 30 天)
Hi, I have attached my code and relevant function files. I have a function that runs if the integer value of the time/diagstep (diagstep is just a step size i have called 10) * diagstep = integer value of time. If it does it runs the function file then adds one onto the counter for this loop which is called diagcounter. I have performed some statistical diagnostics in the statdiag.m function and then plot the mean, variance, skewness etc. My x axis is the diagcounter time steps but I need to have it as time?
So i will need to convert the diagcounter values to work out what time they are so my plot is for example time vs. variance to show how these statistical moments change over time.
I have tried working this out but I am finding it confusing so if anyone had any suggestions or could help that would be great!
Phoebe

回答(1 个)

dpb
dpb 2014-3-15
...My x axis is the diagcounter time steps but I need to have it as time
Why isn't it then just
t=diagcounter*dt;
where dt is the time step. Or, if the timesteps are nonuniform, keep a running sum.
  2 个评论
Phoebe
Phoebe 2014-3-17
the time steps are uniform yes they are just dt=0.01.
have you looked at the attached codes?
where would this t=diagcounter*dt line go?
thanks for your help.
dpb
dpb 2014-3-17
编辑:dpb 2014-3-17
A) No, not really...I don't generally look at what I can't see or long code--post short, pertinent sections specific to the question.
B) Anywhere before you need it after you define the t vector. If you only need it for plotting to get the axis right then it can even be a temporary there such as
plot(diagcounter*dt,y)
ADDENDUM:
In a brief look at the first file I see where it looks like you're computing a t as the summation of dt and doing the plotting a point-at-a-time. In that case, just use the t with the variable to be plotted as the x,y coordinates of the point you're adding.
Or, since it's a fixed delta-t, you could "cheat" leaving the plotting as it currently is then fixing up the plot(s) after the fact--
t=get(gca,'xdata')*dt; % will be the 1:N observations * the dt step
set(gca,'xdata',tt) % set the x-axis to it
xlim([min(t) max(t)]) % reset the x-limits to match
You may want to "cleanup" the last depending on the values you've got--like
xlim([min(t) ceil(max(t))])
if a small integer number or some rounded set of 10 or other even value if larger.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by