2012-11-26 4 views
2

IDL에 간단한 elseif 문을 얻으려고하고 있는데 시간이 지났습니다. MATLAB 코드는 다음과 같습니다.idl elseif 문제/혼란

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 코드는 간단하지 않으며 구문을 올바르게 읽지 못하는 문제가 있습니다. 도움말 상태 : 구문 표현은 THEN 문 [ELSE 문] 또는 표현은 ENDIF

[ELSE 문 을 ENDELSE BEGIN] 그러나 여러 표현식을 사용하는 방법에 대한 예제를 제공 나던 및 문을 시작하면 경우 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 
+0

값이 임계 값과 같다면, 사건 중 어느 것도 실행되지 않습니다 –

+0

그래, 당신이 IDL이 문제를 유발하는 논리가 아니라 실제 구문 때문입니다. IDL은 코드 시험으로 컴파일 및 실행되지 않습니다. 나는 보여주고있다. – nori

답변

3

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 
+0

감사합니다! 미안 해요. 답장이 너무 늦었 어. 방학 중이었고 일하러 돌아 왔어. 해피 2013! – nori

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 
0

mgalloy의 대답이 올바른지,하지만 당신은 단지 하나의 선이있을 때/ENDIF를 시작 사용하지 않는 (나 같은) 사람을 볼 수 있습니다보십시오. (물론 누군가가 돌아가서 자신이 한 일을 깨닫지 못하여 라인을 삽입하려고하면 문제가 발생합니다. 따라서 Michael의 접근 방식은 아마도 더 좋을 것입니다 ... 이것은이 형식을 볼 때 그 일을 실현한다는 것입니다. 같은 일 :

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' 
+0

감사합니다. Joe, 정말 고맙습니다. 건배! – nori