matlabsimulink

Obtain origin of warning issued by m-functions with code generation


I have a large Simulink model consisting of several subsystems, piping sections for example. Each subsystem is modeled with a m-function. Within the m-function I throw warnings (for now I use matlab warnings but I could only print the information) when specific conditions occur in the pipings.

I would like to trace the warning message to the full path of the block calling the m-function, in order to inform the user of which piping is causing the trouble. I have tried using gcb in my m-functions but it is not supported by the code generation capabilities of Simulink, which I need.

Is there another approach to get the whole trace of a warning in this use case ?


Solution

  • I managed pass an identifier to the MATLAB Function blocks allowing me to trace the origin of prints or warnings afterwards, by following these steps :

    1. Create a mask parameter on top of the MATLAB Function.
    2. Pass the parameter as input to the MATLAB function in the function definition. This step requires specifying the entry as a Parameter Data within the Modeling/Symbols Pane panel.
    3. Run a script that set up the identifier for each of the MATLAB Function blocks.
    4. When the model runs, and displays warnings it prints the identifier as well as the warning for debugging purposes.

    Note : I did not manage to pass the path to the block (model_name/subsystem/subsytem2/etc...) as a string as intended, but instead relied on the fact that I have a numerical identifier for each block of the model, which is specific to my application.

    I believe this can be improved, maybe with a more generic answer, but this has solved my issue in my application for now.