using MATLAB
restart_default_msession() # Open a default MATLAB session
x = linspace(-10., 10., 500)
@mput x # put x to MATLAB's workspace@matlab plot(x, sin(x)) # evaluate a MATLAB function
x = linspace(-5., 5. 100)y = x@mput x y@matlab begin (xx, yy) = meshgrid(x, y) mesh(xx, yy, xx.^2 + yy.^2)
end
xx = jarray(get_mvariable(:xx))
yy = jarray(get_mvariable(:yy))
close_default_msession() # close the default session (optional)
x = get_mvariable(:a) # return an instance of MxArray
a = jarray(x) # makes a view of x (using pointer_to_array internally)
... # do stuff on a
delete(x) # free the memory in xViral,Thanks for the suggestion. Actually, I initially provided a @mget macro (it is easy to write one). But I am not completely sure what its behavior should be.There are actually two issues related to converting MATLAB variables to Julia variables:First, it is the issue of memory management. When the C-API mxGetVariable of MATLAB makes a deep copy of the variable to returns it. Then it will leave the Julia code to manage its life cycle. For the sake of performance, the function jarray only make a view of the returned object instead of doing another deep copy. So people have to write code as followsx = get_mvariable(:a) # return an instance of MxArray
a = jarray(x) # makes a view of x (using pointer_to_array internally)
... # do stuff on a
delete(x) # free the memory in x
If there is a way to attach x (an use-defined object) to a (an instance of Array), then I can use finalizer to simplify the memory management cycle though. Otherwise the finalizer associated with a doesn't know what to delete.Second, the mapping from MATLAB to Julia is one-to-one.
@mget x::String
@mget x::Vector
@mget x::Number
etc to specify the type that you want to get back? If no type is specified, you could default to the most general representation of an object, often a Matrix.
I created a package MATLAB.jl (https://github.com/lindahua/MATLAB.jl).This package allows users to call MATLAB functions from within Julia, thus making it easier to use the sheer amount of toolboxes available in MATLAB. I also believe this would be helpful for people to are migrating from MATLAB to Julia.
Hi, StevenI was aware of such functions.Unfortunately, all those functions with prefix mex (e.g. mexCallMATLAB) is supposed to be called only within a mex function. Though not taking a MATLAB session as an argument, such functions actually have quite complex interactions with the host session.
@mput x y@matlab begin u = x + y v = x - yend@mget u v@mget u::Vector v::Array