_Matlab-interface_ ================== Requirements ------------ Matlab, c-compiler and plt-scheme. The interface is written in ansi-c, to make it work in windows and unix/linux. Environment-Variables --------------------- The environment-variables of c-compiler and matlab must be set properly to make sure that: (a) plt-scheme finds the c-compiler to compile and link the matlab-interface. (b) matlab-interface finds matlab to launch the matlab-engine. Example-autoexec.bat: PATH=%PATH%;c:\bin\matlab\bin\win32 <-- matlab PATH=%PATH%;c:\cygwin\bin <-- gcc or ... "CALL C:\PROGRA~1\MICROS~1\VC98\BIN\Vcvars32.bat" <-- ... msvc Installation ------------ (1) Unpack the file 'matlab-interface.plt'. (a) Put 'matlab-interface.plt' into the plt-directory . (b) Either double-click on 'matlab-interface.plt' or tell your shell: cd "Setup PLT" matlab-interface.plt The directory /collects/matlab-interface/ is created and all necessary files are put into it. (2) Open 'info.ss' from /collects/matlab-interface/ in your favourite editor. Find your local matlab-installation and adjust the following two values in 'info.ss' to your personal matlab-configuration: (a) _matlab-include-directory_ -------------------------- 'matlab-include-directory' is the complete path to the directory that contains the file 'engine.h'. The headerfile 'engine.h' is neccessary for successful compilation of the matlab-interface and can be found somewhere inside the matlab-directory. (b) _matlab-link-libraries_ ----------------------- 'matlab-link-libraries' is a list of complete paths to the libraries that have to be linked with an object-file to produce a matlab-engine-application. These libraries are neccessary for successful linking of the matlab-interface and can be found somewhere inside the matlab-directory. Save 'info.ss'. (3) Launch MzScheme or DrScheme and type: (require (lib "installer.ss" "matlab-interface")) (install) ;; start compilation and linking The file 'primitive.c' in /collects/matlab-interface/ is compiled and linked to produce an extension-program which is put inside /collects/matlab-interface/compiled/. Usage - example --------------- (require (lib "primitive.ss" "matlab-interface")) (require (lib "highlevel.ss" "matlab-interface")) (engine-open) (display (engine-eval "magic (4)")) (engine-close) Better examples in directory collects/matlab-interface/examples/. As I'm collecting more of them ... please contribute yours and I'll include them in the next version. Overview - definitions ---------------------- The matlab-interface supports low-level array-manipulation like make-array, array-ref, array-set! and friends. Those are for the ambitious user, who wants to build his own highlevel-procedures. In general, you only need two types of functions: communicators and converters: - Communicators: engine-open, -close, -put, -get, -eval, -apply - Converters to array: string->, object->, vector->, matrix->array - Converters to scheme: array->string, ->object, ->vector, ->matrix Engine-eval is easy to use in rapid interactive experimentation. Engine-apply performs error-checking for use in robust applications. Definitions in _primitive.ss_ ----------------------------- Load the definitions (require (lib "primitive.ss" "matlab-interface")) Abbreviations vector of fixnums vector of fixnums symbol, one of '(cell char double int32) default: 'double symbol, one of '(real complex) default: 'real nested vectors, that implement multidimensional array in scheme Make Array > (make-cell-array [object]) -> array > (make-char-array [object]) -> array > (make-array [type] [complexity] [object]) -> array > (array-clone ) -> array array-name -rank -size -shape -set! -ref and -fill! > (array-name [string]) -> string > (array-rank ) -> integer > (array-size ) -> integer > (array-shape ) -> shape > (array-set! ) -> unspecified > (array-ref value > (array-fill! ) -> unspecified Array-type and complexity > (array-type ) -> symbol > (array-complexity ) -> symbol Predicates > (array? ) -> boolean > (empty-array? ) -> boolean > (real-array? ) -> boolean > (complex-array? ) -> boolean > (cell-array? ) -> boolean > (char-array? ) -> boolean > (double-array? ) -> boolean > (int32-array? ) -> boolean > (numeric-array? ) -> boolean Communicate with matlab-'engine'-process > (engine-open) -> unspecified > (engine-close) -> unspecified > (engine-eval ) -> string | void > (engine-put [string]) -> unspecified > (engine-get ) -> array Convert string<->array vector<->array object<->array matrix<->array > (array->string ) -> string > (string->array ) -> array > (array->object ) -> object > (object->array [type] [complexity]) -> array > (array->vector ) -> vector > (vector->array [type] [complexity]) -> array > (array->matrix ) -> matrix > (matrix->array [type] [complexity]) -> array Info > (print-matlab-array-info) -> unspecified Definitions in _highlevel.ss_ ----------------------------- Load the definitions (require (lib "highlevel.ss" "matlab-interface")) > (engine-apply ... (list ...)) -> Call matlab-function , which is a string, with s as arguments. The syntax is like scheme apply. To facilitate error-catching, the matlab-function must return exactly _one_ value. Two or more values can be packed into one cell-array. Do not use names that begin with 'scheme_' in matlab. These names would get overwritten and deleted when executing 'engine-apply'. No other names are in danger. Regards, Sebastian Sebastian.H.Seidel@med.uni-giessen.de PS: a little demo-program ;; The tools of the trade. (require (lib "primitive.ss" "matlab-interface") (lib "highlevel.ss" "matlab-interface")) ;; Three, two, one, zero ... (engine-open) ;; Visual contact! (array->string (engine-apply "fliplr" (string->array "baltam steem emehcszm") '() )) ;; Map a procedure on a vector. ;; Return the vector. (define vector:map-value! (lambda (p v) (let F ((x (- (vector-length v) 1))) (and (<= 0 x) (begin (vector-set! v x (p (vector-ref v x))) (F (- x 1)))) v))) ;; Map a procedure on a matrix. ;; Return the matrix. (define 2d-matrix:map-index! (lambda (p mx) (let F ((y (- (vector-length mx) 1))) (and (<= 0 y) (begin (let G ((x (- (vector-length (vector-ref mx y)) 1))) (and (<= 0 x) (begin (vector-set! (vector-ref mx y) x (p x y)) (G (- x 1))))) (F (- y 1)))) mx))) ;; Make a matrix. (define matrix (2d-matrix:map-index! (lambda (x y) (+ (sin (/ x (/ 10 3.14159265))) (sin (/ y (/ 10 3.14159265))))) (vector:map-value! make-vector (make-vector 10 10)))) ;; Surf the matrix in matlab. (begin (engine-put (matrix->array matrix) "mx") (engine-eval "surf (mx);") (engine-eval "pause (3);")) ;; Good bye! (engine-close)