Engee documentation
Notebook

Using MATLAB inside an Engee Function block

In this example, we will look at how to use MATLAB code inserts inside an Engee Function block. To do this, we'll use persistent variables, which allow us to extend the functionality of MATLAB code and store values in themselves even after exiting the function. For this demonstration, we have written a counter function in MATLAB, which is reset if the value 1 comes to the input. The code of the function is given below.

image.png

Next, let's test the work of this function. To do this, connect MATLAB and go to the folder with the function.

In [ ]:
using MATLAB
mat"cd $(@__DIR__)"
In [ ]:
mat"cnt(0)"
mat"cnt(0)"
mat"cnt(0)"
Out[0]:
3.0
In [ ]:
mat"cnt(1)"
Out[0]:
0.0

As we can see, the function works correctly every time we call it. With the parameter rst=0 the counter is incremented, and with rst=1 it is reset.

Now that we have tested the function itself, let's add an auxiliary function to run the model and create a model where we will apply this counter.

In [ ]:
# Подключение вспомогательной функции запуска модели.
function run_model( name_model)
    
    Path = (@__DIR__) * "/" * name_model * ".engee"
    
    if name_model in [m.name for m in engee.get_all_models()] # Проверка условия загрузки модели в ядро
        model = engee.open( name_model ) # Открыть модель
        model_output = engee.run( model, verbose=true ); # Запустить модель
    else
        model = engee.load( Path, force=true ) # Загрузить модель
        model_output = engee.run( model, verbose=true ); # Запустить модель
        engee.close( name_model, force=true ); # Закрыть модель
    end
    sleep(5)
    return model_output
end
Out[0]:
run_model (generic function with 1 method)

The figure below shows the model we have developed.

image.png

Now let's analyse the code presented in the Engee Function block.

image_2.png

So, the sequence of actions: connect MATLAB library, then explicitly specify the path to our counter file and call it, feeding the result to the block output.

Now let's describe the loop in which we will call our model. In it, the counter will be reset on the first and third runs of the model.

In [ ]:
rst = 0;
for i in 1:1:5
    if i == 1 || i == 3
        rst = 1;
    else
        rst = 0;
    end
    run_model("MATLAB_in_Engee_models") # Запуск модели.
    c = collect(Cnt)
    print("Значение счётчика на "*string(i)*" запуске модели: "*string(c.value))
end
Building...
Progress 0%
Progress 100%
Progress 100%
Значение счётчка на 1 запуске модели: [0.0]Building...
Progress 0%
Progress 100%
Progress 100%
Значение счётчка на 2 запуске модели: [1.0]Building...
Progress 0%
Progress 100%
Progress 100%
Значение счётчка на 3 запуске модели: [0.0]Building...
Progress 0%
Progress 100%
Progress 100%
Значение счётчка на 4 запуске модели: [1.0]Building...
Progress 0%
Progress 100%
Progress 100%
Значение счётчка на 5 запуске модели: [2.0]

As a result, our function works correctly: in those runs when we set "rst=1", the counter is equal to zero.

Conclusion

We have shown how to use MATLAB code inserts in the Engee Function block and also demonstrated the possibility of connecting MATLAB files to Engee Function. As you can see, these functions are easy to use and they work correctly.

Blocks used in example