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

2 次查看(过去 30 天)
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

回答(1 个)

Sayyed Ahmad
Sayyed Ahmad 2020-6-16
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 个评论
Peter MacDonald
Peter MacDonald 2020-6-16
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 2020-6-16
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by