27 Nov 2012 01:27 AM |
|
I am trying to get a simple elseif statement into IDL and am having a heck of a time with it. The working matlab code looks like this.
a = 1
b = 0.5
diff = a-b
thres1 = 1
thres2 = -1
if diff > thres1
'case 1'
elseif diff
But the IDL code is not so simple, I get compile errors and I am having troubles getting the syntax right.
the help states the syntax is:
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 aka multiple conditions or elseif. I have tried many variations and cant seem to get it right.
Anyone have suggestions? Here are some examples of code i have tried in IDL and neither of them work.
if (diff gt thres1) then begin
print, 'case 1'
endif else begin (diff lt thres2)
print, 'case 2'
endif else begin
print, 'case 3'
endelse
if (diff gt thres1) then begin
print, 'case 1'
else (diff lt thres2) then
print, 'case 2'
else
print, 'case 3'
endelse
endif
|
|
|
|
Deleted User New Member
Posts:  
27 Nov 2012 06:06 AM |
|
So I figured it out. For those of us that are new to the IDL language.
It appears to me that IDL can only handle 2 cases for each if statement, so I had to write in another 'if' block.
hope this helps someone out there.
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
|
|
|
|
Deleted User New Member
Posts:  
27 Nov 2012 08:25 AM |
|
Sometimes you will find this easier to write in IDL as a CASE statement:
a = 1
b = 0.5
diff = a-b
thres1 = 1
thres2 = -1
case 1 of
diff gt thres1: 'case 1'
diff lt thres2: 'case 2'
else: 'case 3'
endcase
|
|
|
|
Deleted User New Member
Posts:  
29 Nov 2012 05:40 AM |
|
HI Dave,
Thanks for clearing that up. I was just testing my code and found that the if statement wasnt working because 'case 3' was being tested in the second half of the code.
And funny thing, i was just on your site and going through your book looking for some hints! Perhaps you could add this suggestion in your idl programming techniques book in the if...then...else section? ie-for multiple conditions use the CASE.
Thanks again and your book is worth every penny.
It would also be great if the IDL help docs could add such a statement as well!
edit: I am assuming your Dave Fanning aka idlcoyote.com, if not sorry.
|
|
|
|
Deleted User New Member
Posts:  
29 Nov 2012 08:38 AM |
|
You have the right person. I don't usually hang out here, but I was passing through. You can find me almost every day over on the IDL newsgroup: comp.lang.idl-pvwave. Cheers!
|
|
|
|