connect variable signals with signal builder or equivalent block using M Script
14 次查看(过去 30 天)
显示 更早的评论
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:
- A model file containing inport blocks
- 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 ?
0 个评论
采纳的回答
Keshav
2024-7-8
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Model Editing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!