Differences between revisions 1 and 2
Revision 1 as of 2009-04-06 20:33:01
Size: 1932
Editor: anonymous
Comment:
Revision 2 as of 2009-04-06 21:03:15
Size: 2308
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
== Do something in MatLab == Do something in MatLab ==
Line 29: Line 29:
}}} }}}
Line 31: Line 32:

{{{
Line 41: Line 44:
==== rinit ====
Initialize APL connection to R COM Server.
==== mlinit ====
Initialize APL connection to MatLab COM Server.
Line 45: Line 48:
     ∇ rinit
[1] '#.R'⎕WC'OleClient' 'StatConnectorSrv.StatConnector'
[2] #.R.Init('R')
     ∇ mlinit
[1] ⍝ version for ⎕SE
[2] '#.MatLab'⎕WC'OleClient' 'Matlab.Application'
[3] 2 ⎕NQ'#.MatLab' 'SetMethodInfo' 'PutFullMatrix'(('' 'VT_VOID')('Name' 'VT_BSTR')('Workspace' 'VT_BSTR')('pr' 'VT_ARRAY of VT_R8')('pi' 'VT_ARRAY of VT_R8'))
Line 50: Line 54:
==== rexec ====
Execute R expression and possibly return the result to APL
==== mlexec ====
Execute MatLab expression and possibly return the result to APL
Line 54: Line 58:
     ∇ a←{r}rexec exp
[1] ⍝ Execute R expression and possibly return the result
[2] ⍝R: R expression ('char')
[3] ⍝L: if 1, then return the result
[4] ⍎(0=⎕NC'r')/'r←0'
[5] a←⍬
[6] :If r
[7] a←#.R.Evaluate(exp)
[8] :Else
[9] #.R.EvaluateNoReturn(exp)
[10] :EndIf
     ∇ {a}←mlexec exp
[1] ⍝ Execute MatLab expression and convert char2num
[2] ⍝R: MatLab expression ('char')
[3] ⍝e: mlexec 'surf(peaks)'
[4] ⍎(';'≠¯1↑exp)/'exp←exp,'';'''
[5] a←#.MatLab.Execute exp
     ∇

}}}
==== mlput ====
Put APL array into MatLab

{{{
     ∇ {m}mlput a;x1
[1] ⍝ Version for ⎕SE
[2] ⍝ Put APL array to MatLab
[3] ⍝R-name of variable to export from APL
[4] ⍝L-name of variable to create in MatLab
[5] ⍎(0=⎕NC'm')/'m←''X''' ⍝ Default
[6] #.MatLab.PutFullMatrix m'base'a 0
[7] a←#.MatLab.Execute m,'=real(',m,');'
Line 67: Line 81:
==== rput ====
Put APL array into R
==== mlget ====
Get MatLab variable (matrix or vector) to APL
Line 71: Line 85:
     ∇ {m}rput a;x1
[1] ⍝ Put APL array into R
[2] ⍝R-name of variable to export from APL
[3] ⍝L-name of variable to create in R
[4] ⍎(0=⎕NC'm')/'m←''x''' ⍝ Default name is x
[5] m←,⊂m
[6] a←,⊂a
[7] #.R.SetSymbol(m,a) ⍝ put var a into R with name m
     ∇ r←mlget x;rho;Re;Im;⎕ML
[1] ⍝ version for ⎕SE
[2] ⍝ Get MatLab matrix to APL
[3] ⍝L-name of variable to import ('char')
[4] ⎕ML←3
[5] rho←#.MatLab.Execute'size(',x,')'
[6] rho←1↓(~rho∊⎕TC)⊂rho
[7] rho←{⍎¨(⍵≠' ')⊂⍵}¨rho
[8] rho←∊rho
[9] Re←rho⍴0 ⍝ Protorype variable
[10] Im←0
[11] r←↑#.MatLab.GetFullMatrix x'base'Re Im
Line 81: Line 99:
==== rget ====
Get R variable (matrix or vector) to APL

{{{
     ∇ r←rget name;rho;Re;Im;⎕ML
[1] ⍝ Get R variable (matrix or vector) to APL
[2] ⍝R-name of variable to import ('char')
[3] ⎕ML←3
[4] r←#.R.GetSymbol(name)
     ∇
}}}

Interface to MatLab

This interface uses buitin MatLab COM (OLE) Server

Sample APL Session

     )load r2apl
d:\r2apl saved Mon Apr 06 22:10:59 2009
      mlinit   ⍝ initialize connection to MatLab COM server
      )obs     ⍝ new object MatLab is created
MatLab
      mlput 2 3⍴⍳6 ⍝ put APL matrix to MatLab

Do something in MatLab

» X

X =

     1     2     3
     4     5     6

» sum(X)

ans =

     5     7     9

» s=sum(X);
» 

== Back to APL ==

      mlget 's'
5 7 9
      mlexec 'surf(peaks)'

And MatLab graphics window pops up:

surf.png

Dyalog APL functions

mlinit

Initialize APL connection to MatLab COM Server.

     ∇ mlinit
[1]    ⍝ version for ⎕SE
[2]    '#.MatLab'⎕WC'OleClient' 'Matlab.Application'
[3]    2 ⎕NQ'#.MatLab' 'SetMethodInfo' 'PutFullMatrix'(('' 'VT_VOID')('Name' 'VT_BSTR')('Workspace' 'VT_BSTR')('pr' 'VT_ARRAY of VT_R8')('pi' 'VT_ARRAY of VT_R8'))

mlexec

Execute MatLab expression and possibly return the result to APL

     ∇ {a}←mlexec exp
[1]    ⍝ Execute MatLab expression and convert char2num
[2]    ⍝R: MatLab expression ('char')
[3]    ⍝e: mlexec 'surf(peaks)'
[4]    ⍎(';'≠¯1↑exp)/'exp←exp,'';'''
[5]    a←#.MatLab.Execute exp

mlput

Put APL array into MatLab

     ∇ {m}mlput a;x1
[1]    ⍝ Version for ⎕SE
[2]    ⍝ Put APL array to MatLab
[3]    ⍝R-name of variable to export from APL
[4]    ⍝L-name of variable to create in MatLab
[5]    ⍎(0=⎕NC'm')/'m←''X''' ⍝ Default
[6]    #.MatLab.PutFullMatrix m'base'a 0
[7]    a←#.MatLab.Execute m,'=real(',m,');'

mlget

Get MatLab variable (matrix or vector) to APL

     ∇ r←mlget x;rho;Re;Im;⎕ML
[1]     ⍝ version for ⎕SE
[2]   ⍝ Get MatLab matrix to APL
[3]   ⍝L-name of variable to import ('char')
[4]    ⎕ML←3
[5]    rho←#.MatLab.Execute'size(',x,')'
[6]    rho←1↓(~rho∊⎕TC)⊂rho
[7]    rho←{⍎¨(⍵≠' ')⊂⍵}¨rho
[8]    rho←∊rho
[9]    Re←rho⍴0 ⍝ Protorype variable
[10]   Im←0
[11]   r←↑#.MatLab.GetFullMatrix x'base'Re Im

ml2apl (last edited 2009-04-07 08:34:53 by KaiJaeger)