Using Julia, MATLAB, and Python together to solve engineering problems
Introduction
A modern engineer and scientist is rarely limited to one programming language. Numerical calculations are based on MATLAB, data analysis is based on Python, and high—performance computing is based on Julia. But what if you combine them instead of choosing them?
The Engee computing environment allows you to run Julia, MATLAB, and Python code directly in a single script, exchanging data between languages on the fly. In this example, we'll show how it works: create a matrix in Julia, process it in MATLAB, save it to a .mat file, and then analyze the result using NumPy — all without going beyond one environment.
Joining libraries
In this example, we will need the following libraries:
MATLAB— to run MATLAB commands and functions directly from Julia, allowing you to share variables between environmentsMAT— for reading and writing files .mat works as an offline data converter without having to run MATLAB itself.PyCall— for Python scripts and libraries from Julia with automatic type conversion between languages.
using MATLAB, MAT, PyCall
Calling MATLAB from Julia
Executing MATLAB commands
This script will be executed inside the MATLAB core.
mat"""
a = 10;
b = 20;
c = a + b;
disp(c);
"""
Calling the MATLAB function
There is a file:
% function1.m\
\
function y = function1(x)\
y = x.^2 + 1;\
end
Let's execute the MATLAB script using the command mxcall()and we will display the result.
result = mxcall(:function1, 1, 5)
println(result)
Data transfer Julia → MATLAB
The macro @mput places Julia variables in the MATLAB workspace.
x = [1,2,3,4,5]
@mput x
mat"""
y = x.^2;
disp(y)
"""
Now MATLAB sees the variable x.
Multiple variables
a = 10
b = 20
@mput a b
mat"""
c = a + b;
disp(c)
"""
Getting MATLAB → Julia data
The macro @mget extracts MATLAB variables and places them in the Julia workspace.
mat"""
x = 42;
"""
@mget x
println(x)
Getting an array
mat"""
A = magic(3);
"""
@mget A
println(A)
Simultaneous data transmission
x = [1.0, 2.0, 3.0]
@mput x
mat"""
y = sin(x);
z = cos(x);
"""
@mget y z
println(y)
println(z)
Transfer of matrices
A = rand(5,5)
@mput A
mat"""
B = inv(A);
"""
@mget B
println(B)
Data exchange without using macros
Through put_variable() and get_variable()
x = 10
put_variable(:x, x)
mat"""
y = x^2;
"""
y = get_variable(:y)
println(y)
Through interpolation $
x = 10
mat"""
y = $x^2;
"""
y = get_variable(:y)
println(y)
Through mxcall()
x = 10
y = mxcall(:sqrt, 1, x)
println(y)
Analog in MATLAB:
y = sqrt(10);
Through an explicit session MSession
s = MSession()
put_variable(s, :x, 10)
eval_string(s, "y = x^2;")
y = get_mvariable(s, :y)
println(y)
close(s)
Using the built-in MATLAB expression
y = mxcall(:power, 1, 10, 2)
println(y)
Analog in MATLAB:
power(10,2)
Calling the MATLAB script
There is a file:
% script1.m
x = 100;
y = x^2;
disp(y)
mat"""
run('script1.m');
"""
or
mat"""
script1
"""
Saving MAT files
Julia → MAT
A = rand(100,100)
matwrite("data.mat", Dict("A" => A))
Then, in MATLAB, you can do:
mat"""
load('data.mat')
"""
MATLAB → Julia
data = matread("data.mat")
A = data["A"]
Calling Python from Julia
Executing a Python script
py"""
x = 10
y = 20
z = x + y
print(z)
"""
Getting the result
result = py"z"
println(result)
Calling a Python function
There is a file:
# mymodule.py
def square(x):
return x*x
pushfirst!(PyVector(pyimport("sys")."path"), ".")
mod = pyimport("mymodule")
result = mod.square(5)
println(result)
Data transfer Julia → Python
a = [1,2,3,4]
py"""
a = $a
"""
Python now sees the vector x.
Transfer of the NumPy matrix
A = rand(3,3)
py"""
A = $A
"""
PyCall automatically converts the array.
Getting Python → Julia data
py"""
import numpy as np
A = np.random.rand(3,3)
"""
A = py"A"
println(A)
Using NumPy directly
np = pyimport("numpy")
x = np.linspace(0, 10, 100)
println(x)
NumPy + Julia-array
A = rand(100)
np.mean(A)
Data transfer between all three languages
Julia ↔ MATLAB ↔ Python
Julia:
x = rand(100)
@mput x
MATLAB:
mat"""
y = fft(x);
save('fft.mat','y')
"""
Julia:
d = matread("fft.mat")
y = d["y"]
Python:
np = pyimport("numpy")
py_y = PyObject(y)
meanval = np.mean(abs.(y))
Calling MATLAB and Python in the same Julia script
x = 1:10
@mput x
mat"""
y = x.^2;
"""
@mget y
np = pyimport("numpy")
avg = np.mean(y)
println(avg)
Here, the data flows through the chain: Julia → MATLAB → Julia → Python → Julia, which is one of the typical scenarios of scientific computing and data analysis.
Conclusion
We have seen that the Engee computing environment is able to seamlessly integrate three language universes into a single computing process, which allows us to unite engineering communities using different programming languages, providing a ready-made platform where MATLAB-compatible calculations, Python scripts and high-performance Julia code work together without having to manually configure interlanguage bridges.