Main Content

slreq.getTextRange, slreq.getTextRanges

Get line ranges

Since R2022b

    Description

    lr = slreq.getTextRange(fileName,lines) returns the line ranges associated with the lines of code, lines, in the file specified by fileName.

    Note

    You must open the file in the MATLAB® Editor before using this function.

    example

    lr = slreq.getTextRange(fileName,blockSID,lines) returns the line ranges associated with the lines in the MATLAB Function block specified by blockSID.

    Note

    You must open the model in Simulink® before using this function.

    example

    lr = slreq.getTextRanges(___) is an alternative way to execute slreq.getTextRange.

    lr = slreq.getTextRange(fileName,ID) returns the line range associated with the ID specified by ID. slreq.getTextRanges does not work for this syntax.

    example

    Examples

    collapse all

    This example shows how to modify line numbers for an slreq.TextRange object.

    Open the myAdd code file and load the myAddRequirements requirement set.

    file = "myAdd.m";
    open(file);
    slreq.load("myAddRequirements");

    Get the slreq.TextRange object associated with the third line in the myAdd function.

    cr = slreq.getTextRange(file,3);

    Get the line numbers associated with the slreq.TextRange object.

    lines = getLineRange(cr)
    lines = 1×2
    
         3     3
    
    

    Associate the slreq.TextRange object with line number 2.

    setLineRange(cr,2)

    Confirm that the slreq.TextRange object is associated with the line by getting the text contents of the line range.

    text = getText(cr)
    text = 
    'y = u + v;'
    

    This example shows how to get slreq.TextRange objects in MATLAB Function blocks.

    Open the myAddModel Simulink® model.

    model = "myAddModel";
    open_system(model);

    Get the SID of the MATLAB Function block and return it as a string.

    block = "myAddModel/MATLAB Function";
    SID = get_param(block,"SID")
    SID = 
    '8'
    

    Get the slreq.TextRange object associated with the first line of the MATLAB Function block.

    cr = slreq.getTextRange(model,SID,1);

    This example shows how to get slreq.TextRange objects by using the value of the ID property.

    Open the myAdd code file.

    file = "myAdd.m";
    open(file);

    Get the slreq.TextRange object associated with the ID 738659.742.1.

    cr = slreq.getTextRange(file,"738659.742.1");

    Input Arguments

    collapse all

    Name of the file containing the lines of code, specified as a string scalar or character vector.

    Example: "myAdd.m"

    Start and end line numbers for the line range, specified as a double array of the form [start end] or a scalar double.

    Example: [1 4], 1

    MATLAB Function block SID, specified as a string scalar or character vector.

    Example: "30"

    Line range ID, specified as a string scalar or character vector. The ID is the Id property of the object.

    Example: "738659.742.1"

    Output Arguments

    collapse all

    Line range, returned as an array of slreq.TextRange objects.

    Tips

    • You can also use the getTextRange method to get code range objects.

    Version History

    Introduced in R2022b