Why isn't my loop working?

조회 수: 2(최근 30일)
Madison Goodwin
Madison Goodwin 2018년 5월 18일
댓글: VBBV 2021년 10월 25일
fid = uigetfile('*.*');
fid = fopen(fid);
deltat = 0.5;
n = 1;
while fgetl(fid)~= -1
line = fgetl(fid);
line = sscanf(line, '%f V, %i counts, %i ms');
vout = line(1);
counts = line(2);
t = line(3)/1000;
t(n+1) = t(n) + deltat;
% Q = flowrate (L/s)
% h = head(m)
% P = pressure
% p = density of water
p = 1000; %kg/m^3);
g = 9.81;
vs = 5;
% vout - get from data
% h - get from data
% Q - calculated from data
P = ((vout/vs) - 0.04)/0.0018;
% Finding head
h = (P/1000)/(p*g);
Q = counts/((t(n)/1000)*330);
% Finding hydraulic power
H =(Q*p*g*h)/1000;
end
  댓글 수: 6
Madison Goodwin
Madison Goodwin 2018년 5월 18일
So if I have these two lines as my code will it update the timestep, the value of line(3) is 500ms. After dividing by 1000 it is 0.5, and deltat is 0.5
t = line(3)/1000;
t = t + deltat;

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

답변(2개)

Greg
Greg 2018년 5월 18일
편집: Greg 2018년 5월 18일
Purely a guess, you goofed up some indexing:
t = line(3)/1000; % <-- Recreates a new "t" as a scalar every loop iteration
t(n+1) = t(n) + deltat; % <-- Therefore, t(n) does not exist, or is 0
Also, I don't see you incrementing n anywhere.

VBBV
VBBV 2021년 10월 25일
Change the line below
while fgetl(fid)~= -1
to
while fid~=-1
  댓글 수: 1
VBBV
VBBV 2021년 10월 25일
Also dont forget to do fclose(fid) after the loop ends

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by