Borrar filtros
Borrar filtros

Database that changes with simulation iteration

1 visualización (últimos 30 días)
Nicholas
Nicholas el 16 de Nov. de 2023
Editada: Nicholas el 21 de Nov. de 2023
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!

Respuestas (1)

Pratik
Pratik el 21 de Nov. de 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.
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!
  1 comentario
Nicholas
Nicholas el 21 de Nov. de 2023
Editada: Nicholas el 21 de Nov. de 2023
Hello Pratik,
Thank you for your answer, I really appreciate it! However, I am having a minor issue trying to implement it: Aparently simulink is unable to interpret cell arrays as the diagnostic says "Error:Unsupported input format for From Workspace block 'scratch_datamanagement/From Workspace'. Input data must use a supported format such as timeseries or timetable and must not contain Inf or NaN values."
I get the above error when trying to import the "database", which is a (1x3) cell where the elements are simply the vector [1, 1, 1].

Iniciar sesión para comentar.

Categorías

Más información sobre Programmatic Model Editing en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by