MATLAB Answers

0

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 at 10:15
Latest activity Answered by Sammit Jain on 18 May 2018 at 11:05

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.

 %Variables
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-');
    drawnow
    handles.plot2 = plot(handles.axes2, Q, t, 'o-');
    drawnow
    handles.plot3 = plot(handles.axes3, h, Q, 'o-');
    drawnow
    handles.plot4 = plot(handles.axes4, H, Q, 'o-');
    drawnow
    handles.data.pressure = P;
    handles.data.time = t;
    handles.data.hpower = H;
    handles.data.flowrate = Q;
    handles.data.head = h;
    %Update handles
    guidata(hObject,handles);
end
 %save changes to the handle
 guidata(hObject,handles);

  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

t_tot=cumsum(t)

Sign in to comment.

1 Answer

Answer by Sammit Jain on 18 May 2018 at 11:05

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.

  0 Comments

Sign in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today