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

조회 수: 5(최근 30일)
답변: Sammit Jain 2018년 5월 18일
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)
% P = pressure
% p = density of water
P = ((new_line(1)/vs) - 0.04)/0.0018;
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;
%Update handles
guidata(hObject,handles);
end
%save changes to the handle
guidata(hObject,handles);
##### 댓글 수: 1표시 없음숨기기 없음
jonas 2018년 5월 18일
편집: jonas 2018년 5월 18일
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)

댓글을 달려면 로그인하십시오.

### 답변(1개)

Sammit Jain 2018년 5월 18일
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.

댓글을 달려면 로그인하십시오.

### 범주

Find more on Loops and Conditional Statements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!