Why isn't my loop working?

1 回表示 (過去 30 日間)
Madison Goodwin
Madison Goodwin 2018 年 5 月 18 日
コメント済み: Madison Goodwin 2018 年 5 月 18 日
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 日
Ok now it is producing all the values, but the time step is not updating as I would like it, it is meant to increase by 0.5 seconds for each line, but for each line in the data the t value is 0.5.
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;

サインインしてコメントする。

回答 (1 件)

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.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by