Help with cell if statement

조회 수: 70 (최근 30일)
Charles verhoog
Charles verhoog 2016년 1월 25일
편집: Guillaume 2016년 1월 25일
Trying to make this code work
if Gas_driven(n) == {'Air'}
R = 286.9;
elseif Gas_driven(n) == 'Argon'
R = 208;
else
'Gas Constant not defined'
end
but it keeps coming up with this error
Undefined function or variable 'Air'.
Error in ExpansionShots (line 40)
if Gas_driven(n) == {Air}
  댓글 수: 1
Stephen23
Stephen23 2016년 1월 25일
편집: Stephen23 2016년 1월 25일
The error message indicates that you are actually calling this ( Air is a variable/function):
if Gas_driven(n) == {Air}
but your code shows that you think that you are calling this ( 'Air' is a string):
if Gas_driven(n) == {'Air'}
Also note that == does not check if two strings are the same: you need to use strcmp or strcmpi, exactly as per Ilham Hardy's answer.

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

답변 (1개)

Ilham Hardy
Ilham Hardy 2016년 1월 25일
Perhaps something like this?
if strcmpi(Gas_driven{n},'Air') == 1
R = 286.9;
elseif strcmpi(Gas_driven{n},'Argon') == 1
R = 208;
else
R = 'Gas Constant not defined';
end

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by