Engee documentation
Notebook

Using MATLAB code in the Engee environment

This example demonstrates how to call MATLAB functions in Engee, and how to apply and compare them to the environment functions.

Building curves in Engee:

Connecting the backend - a method for displaying graphics:

In [ ]:
Pkg.add(["Statistics"])
In [ ]:
using Plots

Generate two signals:

In [ ]:
A = sin.(range(0, 2pi, length=100)')' # синусоида
B = @. sin(A) + 0.1 * randn() # зашумлённая синусоида
C = 1:1:100;

Display two signals on the graph:

In [ ]:
plot(C,A)
plot!(C,B)
Out[0]:

Calculate the correlation between the two signals by first connecting the library of statistical functions:

In [ ]:
using Statistics
engee_cor = cor(A,B)
Out[0]:
1×1 Matrix{Float64}:
 0.9852832893786444

Calling MATLAB code from Engee

From Julia, you can call an arbitrary MATLAB command or function that will return a result.

Let's connect the MATLAB interface:

In [ ]:
using MATLAB

Load signal data from Engee into the MATLAB kernel:

In [ ]:
D = hcat(A,B)
A = D[:,1];
B = D[:,2];
mat"""
A = $A;
B = $B;
R = corrcoef(A,B);
"""

Calculating the correlation between two signals using MATLAB kernel function:

In [ ]:
matlab_cor = mat"R; R()"
Out[0]:
2×2 Matrix{Float64}:
 1.0       0.985283
 0.985283  1.0

Comparison of results and calculation of the absolute error of the methods from MATLAB and Engee:

In [ ]:
difference = abs(engee_cor[1,1] - matlab_cor[1,2])
println("Корреляция Engee: ", engee_cor[1,1], '\n', "Корреляция Matlab: ", matlab_cor[1,2], '\n', "Абсолютная погрешность: ", difference)
Корреляция Engee: 0.9852832893786444
Корреляция Matlab: 0.9852832893786447
Абсолютная погрешность: 3.3306690738754696e-16

Calling a sinusoid with increasing frequency from MATLAB:

In [ ]:
chirp = mat"chirp = dsp.Chirp('InitialFrequency', 0,'SamplesPerFrame', 500); chirp()"
plot(chirp)
Out[0]:

Constructing a magic square:

In [ ]:
mat"magic(3)"
Out[0]:
3×3 Matrix{Float64}:
 8.0  1.0  6.0
 3.0  5.0  7.0
 4.0  9.0  2.0

The simplest arithmetic operations:

In [ ]:
x = range(-10.0, stop=10.0, length=50)
y = range(2.0, stop=3.0, length=50)

mat"""
    $u = $x + $y
    $v = $x - $y
"""

plot(u,v)
>> >> >> 
matlab_jl_2 =

   -8.0000
   -7.5714
   -7.1429
   -6.7143
   -6.2857
   -5.8571
   -5.4286
   -5.0000
   -4.5714
   -4.1429
   -3.7143
   -3.2857
   -2.8571
   -2.4286
   -2.0000
   -1.5714
   -1.1429
   -0.7143
   -0.2857
    0.1429
    0.5714
    1.0000
    1.4286
    1.8571
    2.2857
    2.7143
    3.1429
    3.5714
    4.0000
    4.4286
    4.8571
    5.2857
    5.7143
    6.1429
    6.5714
    7.0000
    7.4286
    7.8571
    8.2857
    8.7143
    9.1429
    9.5714
   10.0000
   10.4286
   10.8571
   11.2857
   11.7143
   12.1429
   12.5714
   13.0000

Out[0]:

Calling MATLAB files from Engee

Path to the folder opened in the file manager:

In [ ]:
mat"cd $(@__DIR__)"

Running m-script:

image.png

In [ ]:
mat"file"
>> >> >> a = 10
In [ ]:
mat"run('file.m')"
>> >> >> a = 10
In [ ]:
mat"""run("file.m")"""
>> >> >> a = 10

Running a MATLAB function:

image.png

In [ ]:
mat"fun(10,9)"
Out[0]:
103.11111111111111
In [ ]:
x = 12;
y = 13.12;

z = mat"fun($x,$y)"

z/10
Out[0]:
17.0

Viewing the MATLAB workspace:

In [ ]:
mat"whos"
>> >> >>   Name                     Size            Bytes  Class        Attributes

  A                      100x1               800  double                 
  B                      100x1               800  double                 
  R                        2x2                32  double                 
  a                        1x1                 8  double                 
  chirp                    1x1                 8  dsp.Chirp              
  matlab_jl_has_ans        1x1                 8  double                 

Clearing the MATLAB workspace:

In [ ]:
mat"clear"
mat"whos"
>> >> >>   Name                   Size            Bytes  Class     Attributes

  matlab_jl_has_ans      1x1                 8  double              

Conclusion:

This example has demonstrated the use of MATLAB functions in the Engee environment, their combined use, and the numerical difference between the methods used.