MATLAB Answers


How to I change the time step in the following loop, so the new time is for the next line of code beign read?

Asked by Madison Goodwin on 18 May 2018
Latest activity Answered by Sammit Jain on 18 May 2018
So I have this data that is a three column vector, which has 30 rows. The data is in the columns: volts, counts, time. The time column all says 500ms for all of the rows. But for the second row I would like the time to be adjusted to 1000ms, then by 1500ms and so on and so forth for all of the other lines of data. How do I do this for inside the loop? Also how do I plot this accordingly, I am using a GUI, through using guide.
p = 1000;
g = 9.81;
vs = 5;
while fgetl(handles.fid)~= -1
line = fgetl(handles.fid);
new_line = sscanf(line, '%f V, %i counts, %i ms');
t = new_line(3)/1000;
% Q = flowrate (L/s)
% h = head(m)
% P = pressure
% p = density of water
P = ((new_line(1)/vs) - 0.04)/0.0018;
% Finding head
h = (P/1000)/(p*g);
Q = new_line(2)/((t/1000)*330);
% Finding hydraulic power
H =(Q*p*g*h)/1000;
%%%%%%%%%%%%%%%%%%%%%Plotting %%%%%%%%%%%%%%%%%%%%%%%%%
hold all
handles.plot = plot(handles.axes1, t, P, 'o-');
handles.plot2 = plot(handles.axes2, Q, t, 'o-');
handles.plot3 = plot(handles.axes3, h, Q, 'o-');
handles.plot4 = plot(handles.axes4, H, Q, 'o-');
drawnow = P; = t; = H; = Q; = h;
%Update handles
%save changes to the handle

  1 Comment

Do you mean that the time vector specifies the time step, and what you want is the total time elapsed?
If so, then you can use

Sign in to comment.

1 Answer

Answer by Sammit Jain on 18 May 2018

To handle this more intuitively, you can initialize a counter outside the loop (say k=0).
Inside your loop you can then make the following change to get the total time elapsed:
t = new_line(3)*k/1000;
This should initialize every iteration with t as the time elapsed till then.


Sign in to comment.