Main Content

Write Callbacks to Analyze Safety Analysis Manager Documents

You can use callbacks in the Safety Analysis Manager to analyze the status of your documents and populate the content by reassigning values or adding flags. You can run callbacks manually or automatically.

Create Callbacks

To create a callback:

  1. Open or create a Safety Analysis Manager document. For more information on creating spreadsheets, see Create Spreadsheets in the Safety Analysis Manager.

  2. In the Analyze section, click Edit Callbacks. The Callbacks Editor window opens.

    The Callbacks Editor window. The window has not script, and the PreLoadFcn option is highlighted in grey.

  3. Enter your script in one or more of these callback types:

    Callback TypeWhen Executed
    PreLoadFcnBefore the spreadsheet loads
    PostLoadFcnAfter the spreadsheet loads
    AnalyzeFcnWhen you click Analyze Spreadsheet or press F5
    PreSaveFcnBefore the spreadsheet is saved
    PostSaveFcnAfter the spreadsheet is saved
    CloseFcnBefore the spreadsheet is closed

  4. Exit the Callbacks Editor window to save the callbacks.

You can write scripts manually in the Callback Editor window or write scripts that execute other MATLAB® files on path.

The AnalyzeFcn callback type includes a default callback that is always enabled. You can also add custom AnalyzeFcn callbacks that you can enable or disable. (since R2024a) See Create and Select Custom Callbacks for Safety Analysis Manager Documents.

Modify Spreadsheets with Keywords

To modify a spreadsheet by using callback, retrieve the Spreadsheet object by using the sfa_spreadsheet keyword. For example, this code retrieves and stores the Spreadsheet object in the variable x.

x = sfa_spreadsheet;

If you write MATLAB files and call them in the callback, the files cannot use this keyword.

Add Flags to Documents

Use flags to visually indicate errors, warnings, and checks. In spreadsheets, you can add multiple flags to cells. You can use callbacks to add flags to your spreadsheets.

To add flags to a spreadsheet by using a callback:

  1. Retrieve the Spreadsheet object by using the sfa_spreadsheet keyword.

  2. Retrieve the cell from the spreadsheet by using the getCell function. The function returns the cell as a SpreadsheetCell object.

  3. Add the flag by using the addFlag function on the SpreadsheetCell object.

Safety Analysis Manager spreadsheets support these flags.

Flag TypeDescription

An error flag. Cells with an error flag display the error icon .


A warning flag. Cells with a warning flag display the warning icon .


A check flag. Cells with a check flag display the check icon .

After you add flags, the Safety Analysis Manager displays the flags in the Properties pane. The Spreadsheet tab displays the cells for the entire spreadsheet, and the Cell tab displays the flags in the selected cell. For example, this image shows the Spreadsheet tab in a Safety Analysis Manager spreadsheet that contains one of each type of flag.

The Spreadsheet tab in a Safety Analysis Manager spreadsheet that has a flag of each type. Each flag has a description just below the link

When you run callbacks in the AnalyzeFcn parameter, the Safety Analysis Manager clears the flags from the document before executing the script. To manually clear the flags without running the callback, in the Analyze section, click Clear Flags. To clear individual flags in spreadsheets, in the Properties pane, in the Cell or Spreadsheet tabs, in the Flags section, select the flag description and press Delete on your keyboard. To delete multiple flags, select the flag type, containing column, or containing row, and press Delete on your keyboard.

Spreadsheet Callback Example

This example runs a callback on a Safety Analysis Manager spreadsheet to flag cells based on their content. Open the FMEA_spreadsheet_example.mldatx file to view the spreadsheet.

The spreadsheet is a Failure Mode and Effects Analysis (FMEA) that assesses possible failures in the volume control of a mixing vessel. The vessel takes in liquid from an inlet pump, mixes it with other products, and then expels the new solution through an outlet pump. The vessel must maintain a steady mixing volume by consistently taking in as much liquid as it expels.

Inspect the RPN Calculation

The FMEA assesses the importance of the failure by calculating the risk priority number (RPN) for each failure mode, its cause, and its likelihood of detection. This number appears in the derived column named RPN. View the column formula to see the code. In the spreadsheet, point to the right side of the column label. Click the arrow and click Edit Formula.

The column formula checks the Severity, Failure Probability, and Detection Rating column values by using the sfa_columnValue operator. See Define Derived Values. If at least one of the column values is empty, then the derived column outputs a message. Otherwise, the column calculates the RPN by taking the product of the values in the Severity, Failure Probability, and Detection Rating columns.

if isempty(sfa_columnValue("Failure Probability")) ||...
          isempty(sfa_columnValue("Severity")) || ...
          isempty(sfa_columnValue("Detection Rating"))
    sfa_derivedValue = "Unable to calculate";
    fp = str2double(sfa_columnValue("Failure Probability"));
    s = str2double(sfa_columnValue("Severity"));
    d = str2double(sfa_columnValue("Detection Rating"));
     sfa_derivedValue = fp*s*d;

Inspect the Callback

The spreadsheet includes an AnalyzeFcn callback that executes when you analyze the spreadsheet. Open the callback to view the code. In the Analyze section, click Edit Callbacks and select AnalyzeFcn. Running the callback adds flags to cells based on these conditions:

  • If the cell in the Verified column is not checked, add a warning flag to it.

  • If the cell in the Severity, Failure Probability, or Detection Rating column does not have a value, add an error flag to the corresponding cell.

  • If at least one of the cells in the Verified, Severity, or Failure Probability columns is empty, do not add a flag to the cell in RPN column of the same row.

  • If the value of the cell in the RPN column is greater than 100, add an error flag to the cell.

  • If the value of the cell in the RPN column is less than or equal to 100 and greater than 35, add a warning flag to the cell.

  • If the value of the cell in the RPN column is less than or equal to 35 add a check flag to the cell.

for rowIndex = 1:sfa_spreadsheet.Rows
% Get the cells to update
   failurePCells = getCell(sfa_spreadsheet,rowIndex,...
          "Failure Probability");
    severeCells = getCell(sfa_spreadsheet,rowIndex,...
    detectCells = getCell(sfa_spreadsheet,rowIndex,...
          "Detection Rating");
    updatedCells = getCell(sfa_spreadsheet,rowIndex,...
          "Failure Mode");
     isVerifiedCells = getCell(sfa_spreadsheet,...
     rpnCells = getCell(sfa_spreadsheet,rowIndex,"RPN");
% Flag rows that are not verified or rated
    if isVerifiedCells.Value == 0
           "Failure mode has not been verified.")
    if isempty(severeCells.Value)
            "Failure mode needs severity rating.")
    if isempty(failurePCells.Value)
            "Failure mode needs probability rating.")
    if isempty(detectCells.Value)
            "Failure mode needs probability rating.")
% Evaluate RPN and assign flags
    if isempty(severeCells.Value) || ...
             isempty(failurePCells.Value) || ...
    elseif str2double(rpnCells.Value) > 100
            "Risk Priority Number exceeds allowable amount." +...
            "Create a mitigation strategy.")
    elseif str2double(rpnCells.Value) <= 100 && ...
            str2double(rpnCells.Value) > 35
            "Risk Priority Number is high." +...
            "Consider a mitigation strategy!")
    elseif str2double(rpnCells.Value) <= 35
            "Risk Priority Number is sufficiently low.")

Analyze the Spreadsheet

Click Analyze Spreadsheet to run the callback.

To view different flag outputs, adjust the cell values and rerun the analysis.

See Also




Related Topics