porting a lookup table into Simulink model

26 visualizaciones (últimos 30 días)
Anirban
Anirban el 25 de Nov. de 2024 a las 18:22
Editada: Rahul el 26 de Nov. de 2024 a las 6:25
We have been working on running a Simulink model through a function file (not script). As part of this effort, we need to port a 100x100 lookup table into the model. This would have to be ported to the model workspace directly through code – we have to avoid any “From Workspace” variables. I tried porting in 2 approaches (1) saving the LUT into a .mat file (2) reading the LUT from an xlsheet. For both cases, the porting was done using SimIn.
In both cases, while running the debugger, I notice the model can read the variables in – the only problem is it is not porting into the model to use (I verified this through a scope). In this space, I also ported the variables and LUT directly into the model workspace using the assignin command, but it is still unable to read.
Could anybody in the august community kindly suggest a way to work around?

Respuestas (1)

Rahul
Rahul el 26 de Nov. de 2024 a las 6:25
Editada: Rahul el 26 de Nov. de 2024 a las 6:25
Hi Anirban,
In order to port Lookup Table data directly to a model’s workspace, using only MATLAB code, you can try the following approach:
If the lookup table data is stored in an Excel (.xlsx) or MAT (.mat) file, you can use MATLAB'sreadmatrixorreadtablefunction to read the data into MATLAB’s base workspace and then assign it to the model workspace, as shown below:
% Read data from the Excel file
lookupTable = readmatrix('LUT_data.xlsx', 'Sheet', 'Sheet1'); % Adjust the sheet name if necessary
% % Alternatively, Read data from the MAT file
% lookupTable = readtable('mars_para_data.mat');
modelWorkspace = get_param('your_model_name', 'ModelWorkspace');
% Assign the data to the model workspace
modelWorkspace.assignin('lookupTable', lookupTable);
Further, using the model’s ‘Simulink.ModelWorkspace’ object, you can verify if the imported LUT data, exists in the given model’s workspace. This can be done using the following script:
% Load your model if it's not already loaded
load_system('your_model_name');
% Get the model workspace handle
modelWorkspace = get_param('your_model_name', 'ModelWorkspace');
% Check if variable exists in the model workspace
exists = modelWorkspace.hasVariable('lookupTable');
if exists
disp('lookupTable exists in the model workspace.');
else
disp('lookupTable does not exist in the model workspace.');
end
Alternatively, you can use the Model Explorer in the Modelling Tab of Simulink, to explore existing variables in the Model Workspace.
To ensure that the data is loaded into the model workspace before the simulation starts, you can consider using model callbacks such asPreLoadFcnorInitFcn. These callbacks can be set in the Model Properties under the Callbacks tab.
For more information regarding the usage of ‘get_param function, you can refer to the following documentation link:
Hope this helps!

Categorías

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

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by