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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!