replace each inports of a simulink model at the root level by constant blocks typed in matlab

main.m
% Define the model name
modelName = 'myModel';

% Load the model
load_system(modelName);

% Get all the blocks in the model
blocks = find_system(modelName, 'Type', 'Block');

% Loop through the blocks
for i = 1:length(blocks)
    % If the block is an Inport block, replace it with a Constant block
    if strcmp(get_param(blocks{i}, 'BlockType'), 'Inport')
        % Get the block's position and delete it
        pos = get_param(blocks{i}, 'Position');
        delete_block(blocks{i});
        
        % Add a Constant block in its place
        constantBlock = add_block('simulink/Sources/Constant', [modelName, '/', sprintf('Constant%d', i)]);
        set_param(constantBlock, 'Position', pos);
    end
end

% Save and close the model
save_system(modelName);
close_system(modelName);
788 chars
27 lines

This code loops through all the blocks in the model and checks if each block is an Inport. If it is, it replaces it with a Constant block. The Constant block is added in the same position as the Inport block using the get_param and set_param functions. Finally, the model is saved and closed.

gistlibby LogSnag