![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1545502/image.png)
Database that changes with simulation iteration
1 view (last 30 days)
Show older comments
In my simulink file, I need a Database that stores vector but I need to be able to add and remove vectors from it. Here it is:
Given a database D, where D = [d(1), d(2), d(3), ..., d(N(k))],
- d(j) is a vector of size n, with n and integer and larger than zero, and j=1,...,N(k);
- N(k) is the number of vectors in my data base at iteration k of my simulation;
- k is the current step I am in my simulation.
When k = 0, I will have an initial database ( e.g. D = [d(1), d(2), d(3), ..., d(N(0))] ), however, as my simulation goes on, I will add or remove vectors from my database (e.g at every step k of my simulation I will need to manipulate this database by adding and/or removing vectors from it), which means the size of D is not fixed.
I have been cracking my head open trying to find a solution to it on simulink but I cannot figure it out. Any ideas?
Cheers!
0 Comments
Answers (1)
Pratik
on 21 Nov 2023
Hi Nicholas,
As per my understanding, you want to create a database that stores vector and can be modified by adding or deleting vector from it. And this must be done at every step of the simulation.
‘MATLAB function’ block can be used to define custom functions in Simulink models by using the MATLAB language. Below is the image of the block for reference.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1545502/image.png)
A code snippet which handles the database can be used within the block. Please refer to the following code snippet for reference:
function D = manageDatabase(D, vectorsToAdd, vectorsToRemove)
% This function manages a dynamic database of vectors.
% D is the current database.
% vectorsToAdd is a cell array containing vectors to be added to the database.
% vectorsToRemove is an array containing indices of vectors to be removed from the database.
% Add new vectors to the database
for i = 1:length(vectorsToAdd)
D{end+1} = vectorsToAdd{i}; % Append new vector to the database
end
% Remove vectors from the database
if ~isempty(vectorsToRemove)
D(vectorsToRemove) = [];
end
end
The array ‘vectorsToAdd’ will have the vectors which need to be added to the database and can be empty when there is no vector to add.
The array ‘vectorsToRemove’ is an array containing indices of vectors to be removed from the database. It will be empty when no vector needs to be removed. Database ‘D’ needs to be initialised in MATLAB workspace before running the simulation.
Please refer to the documentation of ‘MATLAB function’ block for more information:
Hope this helps!
See Also
Categories
Find more on Reporting and Database Access in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!