connect variable signals with signal builder or equivalent block using M Script

12 views (last 30 days)
I have a .mat file that contains various signals and their corresponding values. I also have a model file that contains similar signal names in inport blocks, which are connected to a subsystem. However, the values of these signals in the model file differ from the values in the .mat file.My task is to match the signals of the inport blocks to the signals in the .mat file. If a signal is present in both files, I need to assign the .mat signal and its value to the corresponding inport block in the model. Instead of using the inport block, I need to use a Signal Builder block (or a similar block that I'm not currently aware of).The reason for using a Signal Builder block is that it allows me to assign both the signal value and time to the block. This way, I can directly test the model by running it.In the input files, I will provide:
  1. A model file containing inport blocks
  2. A .mat file
I will write a script that compares the signals in the model file with the signals in the .mat file. If any signals in the model file match the signals in the .mat file, the script will assign the .mat signal values to the corresponding Signal Builder blocks in the model.After making these changes, I will store the updated model file separately without modifying the original model file. it is possible to perform this task and how ?

Accepted Answer

Keshav
Keshav on 8 Jul 2024
Hi,
I understand that you have a Simulink model and a MAT file which contains few signal and their corresponding value. You want to replace all the "Inport" block of your Simulink model with "Signal Builder" block if the corresponding signal exist in the MAT file.
You can try out the following code which will basically iterate over all the "Inports" block in your Simulink model, Check if the signal exist in the MAT file or not and then it will replace the "Inport" block with the "Signal Builder" block
% Step 1: Load the .mat file containing the signals and their values
matFileName = 'signals.mat';
loadedData = load(matFileName);
% Step 2: Open the Simulink model
modelFileName = 'my_model';
open_system(modelFileName);
% Step 3: Identify the Inport blocks and replace them with Signal Builder blocks
inportBlocks = find_system(modelFileName, 'BlockType', 'Inport');
% Loop through each Inport block and replace it with a Signal Builder block
for i = 1:length(inportBlocks)
inportBlock = inportBlocks{i};
inportName = get_param(inportBlock, 'Name');
% Check if the signal exists in the .mat file
if isfield(loadedData, inportName)
% Get the signal data
signalData = loadedData.(inportName);
% Create a new Signal Builder block
signalBuilderBlock = [inportBlock, '_SignalBuilder'];
add_block('simulink/Sources/Signal Builder', signalBuilderBlock);
% Set the position of the Signal Builder block to match the Inport block
inportPosition = get_param(inportBlock, 'Position');
set_param(signalBuilderBlock, 'Position', inportPosition);
% Remove the Inport block
delete_block(inportBlock);
% Assign the signal data to the Signal Builder block
timeData = signalData.time;
valueData = signalData.data;
signalBuilderData = [timeData, valueData];
signalBuilderBlockHandle = get_param(signalBuilderBlock, 'Handle');
signalbuilder(signalBuilderBlockHandle, 'set', 1, 'Signal 1', signalBuilderData);
end
end
% Step 4: Save the modified model as a new file
newModelFileName = [modelFileName, '_modified'];
save_system(modelFileName, newModelFileName);
% Close the model
close_system(modelFileName);
You can run the following command in your MATLAB command window to open the correponding documentation of that function:
add_block:
>> web(fullfile(docroot, 'simulink/slref/add_block.html'))
signalbuilder:
>> web(fullfile(docroot, 'simulink/slref/signalbuilder_cmd.html'))
Thanks
  1 Comment
vivek vala
vivek vala on 8 Jul 2024
Hi Keshav Thank you for the help and fast reply this will really help me
I am facing one problem while loading .mat file as what variable i am having that is having signal name like this (see image or excel attached) (signal and its values not exactly this but somewhat) and its value also present as you can see and below that u can see the timestamps for variable starting with XX0 which is [0.170...,1.790...] for variable starting with XX1_ having timesteps [0.150,1,790] '
So, problem i am facing is while assigning time and its value for that perticular variable to signal builder not able to assign directly.
timeData = signalData.time;
valueData = signalData.data;
this two commands which u provided is not working for me for now
what modifications we need to do in code ?

Sign in to comment.

More Answers (1)

vivek vala
vivek vala on 10 Jul 2024
@Keshav hi keshav can u guide me with my error i have given in comment

Categories

Find more on Interactive Model Editing in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!