2

我正在尝试在 IDL 中添加一个简单的 elseif 语句,并且我玩得很开心。matlab 代码看起来像这样。

a = 1
b = 0.5

diff = a-b
thres1 = 1
thres2 = -1

if diff < thres1 & diff > thres2  
  'case 1'
elseif diff > thres1 
  'case 2'
elseif diff < thres2
  'case 3'
end

但是 IDL 代码并不是那么简单,我在语法正确时遇到了麻烦。帮助说明:语法 IF 表达式 THEN 语句 [ELSE 语句] 或 IF 表达式 THEN BEGIN 语句 ENDIF [ELSE BEGIN 语句 ENDELSE]

但没有给出如何使用多个表达式和 elseif 的示例。我已经尝试了很多变化,但似乎无法做到正确。

有人有建议吗?以下是我尝试过的一些事情:

if (diff lt thres1) and (diff gt thres2) then begin
  print, 'case 1'
endif else begin
if (diff gt thres1) then
  print, 'case 2'
endif else begin
if (diff lt thres2) then
  print, 'case 3'
endif 

if (diff lt thres1) and (diff gt thres2) then begin
  print, 'case 1'
else (diff gt thres1) then
  print, 'case 2'
else (diff lt thres2) then
  print, 'case 3'
endif 
4

3 回答 3

4

IDL中没有elseif声明。尝试:

a = 1
b = 0.5

diff = a - b
thres1 = 1
thres2 = -1

if (diff lt thres1 && diff gt thres2) then begin
  print, 'case 1'
endif else if (diff gt thres1) then begin
  print, 'case 2'
endif else if (diff lt thres2) then begin
  print, 'case 3'
endif
于 2012-12-05T20:51:14.030 回答
0

所以我想通了。对于我们这些不熟悉 IDL 语言的人。

在我看来,IDL 对于每个 if 语句只能处理 2 种情况,所以我不得不在另一个“if”块中编写。

希望这可以帮助那里的人。

a = 1;
b = 2.5;

diff = a-b;
thres1 = 1;
thres2 = -1;

if diff gt thres1 then begin
  print,'case 1'
endif

if (diff lt thres2) then begin
  print,'case 2'
  endif else begin
  print,'case 3'
endelse 
于 2012-11-27T13:09:56.137 回答
0

mgalloy 的回答是正确的,但您可能还会看到一些人(比如我),当只有一行时不使用 begin/endif。(当然,如果有人回去尝试插入一行,却没有意识到你做了什么,这会导致问题,所以迈克尔的方法可能更好......这只是为了当你看到这种格式时,你意识到它正在做一样:

if (diff lt thres1 && diff gt thres2) then $
  print, 'case 1' $
else if (diff gt thres1) then $
  print, 'case 2' $
else if (diff lt thres2) then $
  print, 'case 3'

或可能使某人不太容易插入的格式:

if      (diff lt thres1 && diff gt thres2) then print, 'case 1' $
else if (diff gt thres1)                   then print, 'case 2' $
else if (diff lt thres2)                   then print, 'case 3'
于 2013-01-09T17:14:13.777 回答