Saving data from simulink to Database

7 次查看(过去 30 天)
How can I save the data generated with a simulink model to a database? I have a simulink model that runs cointinuosly and I am taking samples of that data every 5 seconds. I want to store that sampled data to a database, for this I am using a Matlab Function inside my simulink model, the problem Is I dont want to create a new connection to the database everytime I have to store information, instead I want to generate the connection and reuse it everytime I want to write to the database. To be precise, how can I declare a global variable that stores a connection object in simulink, note that Data Store Memory does not work for me since the data types that can be stored are very limited.
This is the function I am using for taking the samples and get the database connection
function [y, conn] = saveToDB(values)
coder.extrinsic('set_param')
alreadySampled = values(9);
time = values(8);
conn = -1;
%Write samples to the database
if mod(time,5) == 0 && alreadySampled == 0
set_param('ActuatorTest_Example_ToFile/alreadySampled', 'Value', '1');
alreadySampled = 1;
conn = getDBConnection();
elseif alreadySampled == 1 && mod(time,5) ~= 0
set_param('ActuatorTest_Example_ToFile/alreadySampled', 'Value', '0');
alreadySampled = 0;
end
y = alreadySampled;
end
function conn = getDBConnection()
global dbConn
if dbConn == 0
dbConn = database('damadics','dlaredorazo','@Dexsys13','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3306/damadics');
else
conn = dbConn;
end
end

回答(1 个)

Rajanya
Rajanya 2024-10-9
I understand that you are trying to store sampled data from a Simulink model in a database after every 5 seconds, without having to create a connection every time the samples need to be written to the database table.
To achieve the same, you can make the connection object ‘persistent’ and make checks to the status of the connection object using the ‘isopen’ function. A sample model and code demonstrating the same is given below:
In this model also, the MATLAB function block determines that the timer value gets written to the database table (‘test_table’) only when it is a multiple of 2. The function takes in two inputs, ‘i’ and ‘u’, where ‘i’ acts as the primary key in the table that the function block is writing to, and ‘u’ is the timer value (‘time’ in the database table).
After making the proper connections and storing it in a connection object ‘conn1’, the following check validates that the connection will not be recreated if it is already active.
if isopen(conn1)
if mod(u,2)==0
data = table(i,u, ...
'VariableNames',{'id' 'time' ...
});
sqlwrite(conn1, 'test_table', data);
disp("Writing to table completed...");
end
else
disp('Database connection is not open.');
%then create the connection...
As the simulation is progressed, the desired outcome is achieved as the connection to the database was created only once when there was no connection object and then the values started getting written to the table after every 2 secs.
You can refer to the documentation links for more information on ‘persistent’ and ‘isopen’ by the following commands:
doc persistent
doc isopen
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Reporting and Database Access 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by