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.
Next, let's test the work of this function. To do this, connect MATLAB and go to the folder with the function.
using MATLAB
mat"cd $(@__DIR__)"
mat"cnt(0)"
mat"cnt(0)"
mat"cnt(0)"
mat"cnt(1)"
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.
# Подключение вспомогательной функции запуска модели.
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
The figure below shows the model we have developed.
Now let's analyse the code presented in the Engee Function block.
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.
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
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.