Why isn't my loop working?
古いコメントを表示
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 件のコメント
Walter Roberson
2018 年 5 月 18 日
Difficult to say without a sample input file.
By the way, remember to fclose(fid) afterwards.
Best line of MATLAB code you'll ever use:
clnfid = onCleanup(@() fclose(fid)); % Immediately following any fopen()
Also, move all of your constants outside the loop, and use fscanf on the fid directly, rather than sscanf(fgetl).
What do you mean "not working?" I don't see any syntax problems, so your loop should be running to the end of the file. Without more information, we can't offer any more than that.
Madison Goodwin
2018 年 5 月 18 日
Greg
2018 年 5 月 18 日
See my answer
Madison Goodwin
2018 年 5 月 18 日
Madison Goodwin
2018 年 5 月 18 日
回答 (1 件)
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.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!