Search code examples
excelmatlabimportexport-to-excelsimulink

Exporting Names of Inputs and Outputs of a Simulink Model to a Excel File


How Can I Export Names of All Inputs and Outputs of a Simulink Model to a Excel File?

I have tried to code a script file but it is nothing more than scratch. I am expecting to import all the inputs and outputs of my Simulink model.


Solution

  • This is fairly simple

    model_name = 'port_test';
    
    mdl = load_system(model_name);
    
    inputs = find_system(mdl,'BlockType','Inport');
    input_names = arrayfun(@(x) get_param(x,'Name'), inputs,'UniformOutput',false);
    
    outputs = find_system(mdl,'BlockType','Outport');
    output_names = arrayfun(@(x) get_param(x,'Name'), inputs,'UniformOutput',false);
    
    • load_system opens the system of interest and returns the model handle
    • find_system is a powerful function (backwards compatible with many versions of MATLAB which will search through a model for blocks/items that match. It will return an array of handles for each object found
    • arrayfun performs a function on an array of items.
    • get_param is used to get the name of each of the items found by find_system

    Two cell arrays (input_names and output_names) will be created and will contain a list of all the inputs and output blocks in the top level of the model specified by model_name.

    For writing out to a spreadsheet, I'd suggest using the power of the writetable function.

    Could be something as simple as this

    input_table = table(input_names);
    writetable(input_table,[model_name '.xlsx'],'Sheet','Inputs');
    
    output_table = table(input_names);
    writetable(output_table,[model_name '.xlsx'],'Sheet','Outputs','WriteMode','append');