map all inputs of a simulink model with channels of a mdf file in matlab

Here's how you can map all inputs of a simulink model with channels of an mdf file in MATLAB:

  1. Load the mdf file using the mdfread function. This will give you a structure containing all the MDF data.
main.m
mdfStruct = mdfread('filename.mdf');
37 chars
2 lines
  1. Load the Simulink model using the load_system function. This will enable you to access the model objects.
main.m
load_system('modelname.slx');
30 chars
2 lines
  1. Get a list of all the input signals in the model using the find_system function.
main.m
inputList = find_system('modelname.slx', 'BlockType', 'Inport');
65 chars
2 lines
  1. Loop through the input signals and map each one with a channel in the MDF file. To do this, you need to extract the signal name from the input object, and then find the corresponding channel in the MDF file by matching the signal name with the channel names.
main.m
for i = 1:length(inputList)
    % Get signal name
    signalName = get_param(inputList{i}, 'Name');
    
    % Find channel in MDF file
    channelIndex = find(strcmp(mdfStruct.channelNames, signalName));
    
    % Map input with channel
    if ~isempty(channelIndex)
        set_param(inputList{i}, 'DataLoggingName', mdfStruct.channelNames{channelIndex});
        set_param(inputList{i}, 'DataLogging', 'on');
    end
end
425 chars
14 lines
  1. Save the Simulink model to apply the changes.
main.m
save_system('modelname.slx');
30 chars
2 lines

This will map all input signals in the Simulink model with channels in the MDF file.

gistlibby LogSnag