How do I connect to a database using a Simulink block created from a custom Matlab class?

2 visualizzazioni (ultimi 30 giorni)
I have created a Matlab class that needs to pull parameters from a database. The class has a function that makes a connection to the database and queries the appropriate data. It is called once, when the class is initialized. The class and connection works when I run it in the command window, however I would like the class to work as a block in a Simulink file.
Below is the code for the function. It was created with the help of the databaseExplorer. It uses commands from the database toolbox
function data = db_load_query(obj)
% This queries the simAC01 database and returns the AC load parameters
% corresponding to the simulation ID
% Make connection to database
conn = database('simAC01','******','*******'); % I stared out the actual credentials
%Set query to execute on the database
query = sprintf(['SELECT P, R, C, Rm, Cm, Tm, Tz ' ...
'FROM simAC01.dbo.configs ' ...
'WHERE id in (SELECT cfg_id ' ...
'FROM simAC01.dbo.loads ' ...
'WHERE sim_id = %d)'],obj.simID);
% Execute query and fetch results
data = fetch(conn,query);
% Close connection to database
close(conn)
% Clear variables
clear conn query
end
When I try and run the class as a simulink block, I get this error:
I have tried running the block in interpreted mode but the error stayed pretty much the same.
I understand that the database function I am currently using can't be generated into code that is compatible with Simulink. I was wondering what functions should I be using or is there a way to make simulink work with the existing code.
Thanks for your time,
Peter

Risposte (1)

Sayyed Ahmad
Sayyed Ahmad il 16 Giu 2020
Try to capsel your class in Matlab level2-s-function.
But think about it, if you use oop in level2-s-function you can use the block in simulink environment but you will not be able to compile this simulink block.
  2 Commenti
Peter MacDonald
Peter MacDonald il 16 Giu 2020
Thanks for the response. I woud prefer to be able to run the model both as a script and a simulink block if that is possible. Is that what you mean about not being able to compile this simulink block?
Sayyed Ahmad
Sayyed Ahmad il 16 Giu 2020
I think it must work.
Matlab Compiler is not able to compile classes, which used in Level-2-S-Function to create machine code (Object Oriented Programming is not supported bei Matlab compiler for Level-2-S-Funtion).
If you are looking to create C or C++ code and put it on a chip, it is better to programming your S-Function as a not OOP matlab code. Otherwise it must work.

Accedi per commentare.

Categorie

Scopri di più su Simulink Functions in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by