Matlab vs. IDL
Matlab vs. IDL
IDL
Michael Liebling Biomedical Imaging Group Institut dImagerie et Optique applique e Swiss Federal Institute of Technology, Lausanne CH - 1015 Lausanne Switzerland [email protected] February 28, 2002
Matlab
x; x(3:5); AB; A.B; A.^2; A^2; % ... A=[1,2,3;4,5,6]; B=A(:,2:3); x=0:9; x=0.0:1.0:9.0; x=byte(0:255); x=0.0:1.0:9.0; sum(x); sum(sum(A)); x z=x;y=2x; size(mat) length(vec) linspace(0,pi,100) fliplr(A) flipud(A) rot90(A) repmat 10*ones(N) for k=1:10, disp(k), end for k=1:N x(k) = k; end if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end switch lower(METHOD) case {linear,bilinear},... disp(linear) case cubic, disp(cubic) case nearest, disp(nearest) otherwise, disp(Unknown) end save image.mat R G B I load image.mat load file.txt -ASCII save result result.txt -ASCII
If Statements
CASE name OF Linda: PRINT, sister John: PRINT, brother Harry: PRINT, step-brother ELSE: PRINT, Not a sibling. ENDCASE SAVE, FILENAME = image.dat, R, G, B,I RESTORE, image.dat OPENW,1,result.txt &$ PRINTF, 1, result, FORMAT=(13F10.3)&$ CLOSE, 1 HELP HELP, x ? indgen
Cases
Save a color image in a le Restore the saved image Restore a matrix stored in ASCII Write a matrix as ASCII
whos whos x who help repmat lookfor spline type repmat why bad = find(data<0) txt=num2str(1.222) eval(sin(x))
Get infos on variables Get infos on variable x Get help Lookfor word spline in help Type the source code If you complain ask for a response Get indices where data meets cond. Convert to text Execute (evaluate) the command Formatted printing print (1,2)
bad = WHERE(data LT 0) txt=STRING(1.222) EXECUTE(SIN(x)) PRINT, 20.*!dpi/4., FORMAT=(F30.18) t=STRING( $ 1,2, FORMAT=("(",I1,",",I2,")"))
File manipulation
IDL
info=ROUTINE INFO(sin) & PRINT, info.path WAIT(3) CD CD, :: PRINT, FINDFILE(*) CD, C=c & PRINT, c Purpose Get info on a function or procedure Perform a 3 second pause Change directory Change to upper directory (MacOS) List les Path of current directory
Matlab
pause(3) cd cd .. ls pwd
Help generation
IDL
MK HTML HELP DOC LIBRARY Purpose
Matlab
Matlab
h=figure(1) text(1,1,1,1)
5
pi
Constants
IDL
!PI, !DPI !RADEG !VALUES.F INFINITY !VALUES.F NAN !VALUES.D INFINITY !VALUES.D NAN A = [1,2,3] B = [4,5,6] C = COMPLEX(A, B) COMPLEX(0, 1.) .RESET SESSION DELVAR, A Purpose 180/ 57.2958 Not a number
Matlab
6
> < == >= <= =
Logics
IDL
GT LT EQ GE LE NE Purpose
Matlab
7.1
A typical M-script le in Matlab looks like this: myplot.m color=rgb x=0:9; y=(x/10.).^2; for k=1:length(color) plot(x,y+k,color(k)) hold on end hold off it is simply launched with the command line: > myplot
In IDL this could be accomplished using a batch le like the one below. mybatch.pro linecolor=[255L,256L(255L),256L(256L*255L)] x=indgen(9) y=x^2 plot, x,y , /nodata FOR k=0,2 DO oplot, x, y/10.+k,$ color=linecolor[k],$ background=255L+256L(256L*255L) Note that there is only a single command per line (in particular, the for construct has no BEGIN and END which is forbidden) and that the $ character has been used to break the oplot command over several lines. A batch le is run with the command: IDL>> @mybatch Another possibility is to use a main .pro le like the one below: mymain.pro linecolor=[255L,256L(255L),256L(256L255L)] x=indgen(9) y=x^2 plot, x,y , /nodata FOR k=1,3 DO BEGIN oplot, x,y, color=linecolor[k] END END (notice the END at the last line) and is run with the command: IDL>> .run mymain or with the equivalent successive commands: IDL>> .compile mymain IDL>> .go mymain
IDL
7.2
Matlab functions can take several arguments (their number may be variable) and can output several variables. stat.m function [mean,stdev] = stat(x) n = length(x); mean = avg(x,n); stdev = sqrt(sum((x-avg(x,n)).^2)/n); function mean = avg(x,n) mean = sum(x)/n;
IDL functions can take several arguments (their number may be variable if they are specied as keywords) but have only one output. Arguments that are passed as keywords are modied inside the function. average.pro FUNCTION AVERAGE, arr RETURN, TOTAL(arr)/N ELEMENTS(arr) END Next function returns the average and puts the standard deviation into variable std if and only if it is specied. average2.pro FUNCTION AVERAGE, arr, STDDEV=std aver=TOTAL(arr)/N ELEMENTS(arr) IF KEYWORD SET(std) THEN std=SQRT(TOTAL((arr-aver)^2))/N ELEMENTS(arr)) RETURN, aver END Still another possibility is to have a procedure. Procedure dont return a result. hello.pro PRO HELLO, name, UP=upcase IF KEYWORD SET(upcase) THEN PRINT, STRUPCASE(name) $ ELSE PRINT, name END
IDL