idl elseif problems/confusion

nori  picture nori · Nov 26, 2012 · Viewed 11.9k times · Source

I am trying to get a simple elseif statement into IDL and am having a heck of a time with it. The matlab code looks something like this.

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

But the IDL code is not so simple and I am having troubles getting the syntax right. the help states: Syntax IF expression THEN statement [ ELSE statement ] or IF expression THEN BEGIN statements ENDIF [ ELSE BEGIN statements ENDELSE ]

But doesnt give an example on how to use multiple expressions and elseif. I have tried many variations and cant seem to get it right.

Anyone have suggestions? Here are some things I've tried:

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 

Answer

mgalloy picture mgalloy · Dec 5, 2012

There is no elseif statement in IDL. Try:

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