JuliaInterop / MATLAB.jl

Calling MATLAB in Julia through MATLAB Engine
MIT License
270 stars 63 forks source link

Function handle #221

Open blegat opened 5 months ago

blegat commented 5 months ago

Is it possible to create a function handle ? I tried the following but it doesn't seem to work

julia> a = MATLAB.mat"@(t, y) 2*t"
inary
Error using save
Unable to save variable 'ans' of class 'function_handle' to STDIO.

ERROR: MATLAB.MEngineError("failed to get variable ans from MATLAB session")
Stacktrace:
 [1] get_mvariable(session::MSession, name::Symbol)
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:164
 [2] get_mvariable
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:168 [inlined]
 [3] get_variable(name::Symbol)
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:170
 [4] top-level scope
   @ ~/.julia/dev/MATLAB/src/matstr.jl:169

However, the following works

julia> eval_string(MSession(), "ode45(@(t,y) 2*t, [0,5], 0)");

Ideally would need to create a function handle that is accessing a large Julia matrix. So while this seems to do the trick, I'm worried that it's just copying the content of the matrix in the string so that won't be so good with a large matrix

julia> x = [1, 2]
2-element Vector{Int64}:
 1
 2

julia> eval_string(MSession(), "ode45(@(t,y) sum($x)*t, [0,5], 0)");

This also does not work as it seems to not be able to represent the function handle in Julia

julia> MATLAB.mxcall(:ode45, MATLAB.mat"@(t, y) 2*t", [0, 5], 0)
Fontconfig warning: "/usr/share/fontconfig/conf.avail/05-reset-dirs-sample.conf", line 6: unknown element "reset-dirs"
Fontconfig warning: "/usr/share/fontconfig/conf.avail/05-reset-dirs-sample.conf", line 6: unknown element "reset-dirs"
Error using save
Unable to save variable 'ans' of class 'function_handle' to STDIO.

ERROR: MATLAB.MEngineError("failed to get variable ans from MATLAB session")
Stacktrace:
 [1] get_mvariable(session::MSession, name::Symbol)
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:164
 [2] get_mvariable
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:168 [inlined]
 [3] get_variable(name::Symbol)
   @ MATLAB ~/.julia/dev/MATLAB/src/engine.jl:170
 [4] top-level scope
   @ ~/.julia/dev/MATLAB/src/matstr.jl:169

Is there any way to call a MATLAB function that requires function handles and that would scale for large matrix. For the context, this is the function I'd like to call https://github.com/alpyurtsever/SketchyCGAL/blob/master/solver/CGAL.m and Primitive1, Primitive2 and Primitive3 are the function handles