Main Content

setCallback

R2026b

Assign code to Safety Analysis Manager document callback

Since R2024a

Description

setCallback(spreadsheet,callbackType,code) assigns the code, code, to the callback, callbackType, in the Safety Analysis Manager spreadsheet, spreadsheet.

example

setCallback(faultTreeDocument,callbackType,code) assigns the code to the callback in the Safety Analysis Manager fault tree document, faultTreeDocument. (since R2026b)

example

Examples

collapse all

Suppose you have one Safety Analysis Manager spreadsheet open, mySpreadsheet.mldatx, and it has two columns. The first is a check box column and the second is a text column. You want to write a callback script where, if the check box for a cell is not selected, you mark the adjacent cell in the text column with a warning flag.

Retrieve the Spreadsheet object of the spreadsheet.

mySpreadsheet = safetyAnalysisMgr.getOpenDocuments;

Create the callback code as a string.

callBackString = "for n = 1:sfa_document.Rows" + newline + ...
"  textCell = getCell(sfa_document,n,2);" + newline + ...
"  checkCell = getCell(sfa_document,n,1);" + newline + ...
"  if checkCell.Value == 0" + newline + ...
"      addFlag(textCell,""warning"");" + newline + ...
"  end" + newline + ...
"end";

The code uses the sfa_document keyword to retrieve the Spreadsheet object of the spreadsheet that contains this script.

Before R2026b: To retrieve the Spreadsheet object in a Safety Analysis Manager callback, use the sfa_spreadsheet keyword.

Assign the code to the default AnalyzeFcn callback.

setCallback(mySpreadsheet,"AnalyzeFcn",callBackString)

Since R2024a

Suppose you have one Safety Analysis Manager spreadsheet open, mySpreadsheet.mldatx, and it has two columns. The first is a check box column and the second is a text column. The spreadsheet also contains a custom callback. You want to define the custom callback script where, if the check box for a cell is not selected, you mark the adjacent cell in the text column with a warning flag.

Retrieve the Spreadsheet object of the spreadsheet.

mySpreadsheet = safetyAnalysisMgr.getOpenDocuments;

Create the callback code as a string.

callBackString = "for n = 1:sfa_document.Rows" + newline + ...
"  textCell = getCell(sfa_document,n,2);" + newline + ...
"  checkCell = getCell(sfa_document,n,1);" + newline + ...
"  if checkCell.Value == 0" + newline + ...
"      addFlag(textCell,""warning"");" + newline + ...
"  end" + newline + ...
"end";

The code uses the sfa_document keyword to retrieve the Spreadsheet object of the spreadsheet that contains this script.

Before R2026b: To retrieve the Spreadsheet object in a Safety Analysis Manager callback, use the sfa_spreadsheet keyword.

Get the name of the custom callback by using the getCustomCallbackNames function (since R2026b).

myCustomCallbackName = getCustomCallbackNames(mySpreadsheet);

Assign the code to the custom callback.

setCallback(mySpreadsheet,myCustomCallbackName(1),callBackString)

Since R2026b

Suppose you have one Safety Analysis Manager fault tree document open, mySpreadsheet.mldatx. You want to check that each event in the fault tree document uses a unique failure model. If the assigned failure model is not shared, add a check flag to the event. If the assigned failure model is shared, add an error flag to the event.

Retrieve the FaultTreeDocument object of the fault tree document.

myFaultTreeDoc = safetyAnalysisMgr.getOpenDocuments;

Create the callback code as a string.

callBackString = "for n = 1:numel(sfa_document.Events)" + newline + ...
"   checkEvent = sfa_document.Events(n);" + newline + ...
"   if sfa_document.Events(n).FailureModel.Shared == 0" + newline + ...
"       addFlag(sfa_document.Events(n),""check"")" + newline + ...
"   else" + newline + ...
"       addFlag(sfa_document.Events(n),""error"")" + newline + ...
"    end" + newline + ...
"end";

Assign the code to the default AnalyzeFcn callback.

setCallback(myFaultTreeDoc,"AnalyzeFcn",callBackString)

Input Arguments

collapse all

Spreadsheet in the Safety Analysis Manager, specified as a Spreadsheet object.

Fault tree document in the Safety Analysis Manager, specified as a FaultTreeDocument object.

Callback type to assign the code to, specified as one of these values:

Callback TypeWhen Callback Executes
"PreloadFcn"Before the document loads
"PostloadFcn"After the document loads
"AnalyzeFcn"When you use the runAnalysis function or press F5. This corresponds to the default AnalyzeFcn callback. To set callback code for a custom callback, set callbackType to the custom callback name.
"PresaveFcn"Before the document is saved
"PostsaveFcn"After the document is saved
"CloseFcn"Before the document is closed
Custom callback nameIf the custom callback is enabled, it executes when you use the runAnalysis function or press F5. Create the custom callback by using the addCallback function.

In R2026a: The noncustom callback types are case insensitive.

Callback code, specified as a character vector or a string scalar.

Data Types: char | string

Version History

Introduced in R2024a

expand all