Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

I don't know what is wrong with my code when recreating a table

조회 수: 1 (최근 30일)
Sarah Kneer
Sarah Kneer 2020년 11월 12일
마감: MATLAB Answer Bot 2021년 8월 20일
There's this table I need to create, in which, depending on the time(t), the value of Clo and Met changes. My code is:
t = 0:0.5:24;
for i = 0:0.5:24
if t(i)>=0 && t(i)<8
Clo = 3;
Met = 0.4;
end
if t(i)>=8 && t(i)<12
Clo = 1;
Met = 1;
elseif t(i)>=12 && t(i)<17
Clo = 0.5;
Met = 1.6;
elseif t(i)>=17 && t(i)<22
Clo = 1;
Met = 0.9;
elseif t(i)>=22 && t(i)<24
Clo = 2.5;
Met = 0.4;
else
plot(t,Clo,'g');
xlabel ('Time (hours)');
ylabel ('Clo');
end
end

답변 (1개)

Subhadeep Koley
Subhadeep Koley 2020년 11월 12일
loop indices must be positive integers or logical values. In your code, you're trying to use fraction values as index. Also, your plot command is inside an else branch. The below modifications might help.
t = 0:0.5:24;
Clo = zeros(size(t));
Met = zeros(size(t));
for idx = 1:numel(t)
if t(idx)>=0 && t(idx)<8
Clo(idx) = 3;
Met(idx) = 0.4;
elseif t(idx)>=8 && t(idx)<12
Clo(idx) = 1;
Met(idx) = 1;
elseif t(idx)>=12 && t(idx)<17
Clo(idx) = 0.5;
Met(idx) = 1.6;
elseif t(idx)>=17 && t(idx)<22
Clo(idx) = 1;
Met(idx) = 0.9;
elseif t(idx)>=22 && t(idx)<=24
Clo(idx) = 2.5;
Met(idx) = 0.4;
end
end
plot(t, Clo,'g')
xlabel ('Time (hours)')
ylabel ('Clo')
figure
plot(t, Met,'r')
xlabel ('Time (hours)')
ylabel ('Met')
  댓글 수: 6
Sarah Kneer
Sarah Kneer 2020년 11월 12일
I got it know, I missed the (i) after Clo and Met. Thanks.
Peter Perkins
Peter Perkins 2020년 11월 19일
It's also worth noting that the loop in this code is unnecessary:
t = 0:0.5:24;
Clo = zeros(size(t));
Met = zeros(size(t));
i = t>=0 & t<8
Clo(i) = 3;
Met(i) = 0.4;
i = t>=8 && t<12
Clo(i) = 1;
Met(i) = 1;
i = t>=12 && t<17
Clo(i) = 0.5;
Met(i) = 1.6;
i = t>=17 && t<22
Clo(i) = 1;
Met(i) = 0.9;
i = t>=22 && t<=24
Clo(i) = 2.5;
Met(i) = 0.4;

태그

Community Treasure Hunt

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

Start Hunting!

Translated by