Create and Configure Safety Analysis Manager Spreadsheets Programmatically
You can create and update Safety Analysis Manager spreadsheets programmatically. For more information on creating Safety Analysis Manager spreadsheets, see Create Spreadsheets in the Safety Analysis Manager. In this example, you create a new spreadsheet, add rows and columns, define column types, define a column formula, and add content to cells.
Create and View New Spreadsheet
To create a new empty spreadsheet to configure, use the safetyAnalysisMgr.newSpreadsheet
function and retrieve the Spreadsheet
object.
mySpreadsheet = safetyAnalysisMgr.newSpreadsheet;
To open the Safety Analysis Manager, use the safetyAnalysisMgr.openManager
function.
safetyAnalysisMgr.openManager
Add Rows and Columns
Add rows and columns with the addRow
and addColumn
functions. In this example, add three rows and seven columns.
Make the last column a derived column.
addRow(mySpreadsheet,Count=3)
addColumn(mySpreadsheet,Count=6)
addColumn(mySpreadsheet,Type="derived")
If you want to update the column type, you must use the graphical interface. See Specify Column Types.
The spreadsheet now has four rows and eight columns. To confirm the number of columns
and rows, you can access the Columns
and Rows
properties of the Spreadsheet
object. If you add too many rows or
columns, use the deleteRow
and deleteColumn
functions.
The columns currently use the default labels. Set the column labels by using the
setColumnLabel
function.
myLabels = ["Failure Mode", ... "Effect","Severity","Potential Failure Cause", ... "Failure Probability","Detection Method", ... "Detection Rating","Info Check"]; for n = 1:length(myLabels) setColumnLabel(mySpreadsheet,n,myLabels(n)) end
Define the Derived Column Formula
The Info Check
column does not have a formula. To define the
formula, use the setColumnFormula
function. In this example, set the formula to
output the message not enough data
when at least one cell in the
row is empty, and output data filled
when each of the cells in
the row has data. To define the formula, write the code as a string.
script = ... "labels = string(keys(sfa_columnValue));" + newline + ... "for n = 1:length(labels)" + newline + ... " if isempty(sfa_columnValue(labels(n)))" + newline + ... " sfa_derivedValue = ""not enough data"";" + newline + ... " else" + newline + ... " sfa_derivedValue = ""data filled"";" + newline + ... " end" + newline + ... "end"; setColumnFormula(mySpreadsheet,"Info Check",script)
Add Content to Cells
To add content to cells, retrieve the SpreadsheetCell
objects from the spreadsheet by using the getCell
function. In this example, add values to the spreadsheet cells
in the Effect
column. Specify each cell value as a character vector
or string scalar by modifying the Value
property of each
SpreadsheetCell
object.
values = ["2", "5", "6", "9"]; for n = 1:length(values) cell = getCell(mySpreadsheet,n,"Effect"); cell.Value = values(n); end
You can repeat this method for each column using string arrays.
Save the Spreadsheet
Save the spreadsheet as a file named newSpreadsheet
in the working
directory.
save(mySpreadsheet,"newSpreadsheet.mldatx")
Create Links
If you have Requirements Toolbox™, you can create links to spreadsheet cells and rows from other model artifacts after saving the spreadsheet. For more information, see Link Safety Analysis Manager Spreadsheet Cells and Rows to Linkable Items.