Main Content

getLineColumn

Find locations of beginning and end of MATLAB code involved in code generation

Description

example

[startLoc,endLoc] = getLineColumn(obj) returns the line and column indices of the first and last character of the MATLAB® code described by obj in the text of the file containing the code. The code described by obj is a MATLAB function or method that is involved in code generation.

example

[startLoc,endLoc] = getLineColumn(obj_message) returns the line and column indices of the first and last character of the MATLAB code that caused the code generation message described by obj_message.

[startLoc,endLoc] = getLineColumn(obj_callsite) returns the line and column indices of the first and last character of the function call site described by obj_callsite in the text of the file containing the call.

Examples

collapse all

Create a report information object for a code generation process. You then locate a MATLAB function involved in code generation in the file containing that function.

Define the MATLAB function foo:

function [b,c] = foo(a)
b = svd(a,0);
c = bar(a);
end

function c = bar(a)
c = inv(a);
end

Generate a static C library for foo. Specify the input as a string scalar. Export the code generation report information to the variable info in your base MATLAB workspace.

codegen -config:lib foo -args {"A string scalar"} -reportinfo info

Code generation fails because a string scalar is not a valid input for the MATLAB functions svd and inv. The code generator creates a report information object info in the base MATLAB workspace.

The property info.Functions is a two-dimensional array. info.Functions(1) contains the description of the MATLAB function foo. info.Functions(2) contains the description of the MATLAB function bar.

To manually inspect the function bar, first display the text of the file containing bar.

info.Functions(2).File.Text
    'function [b,c] = foo(a)
     b = svd(a,0);
     c = bar(a);
     end
     
     function c = bar(a)
     c = inv(a);
     end
     '

Use getLineColumn to locate the beginning and end of the function bar in this text. The output startLoc contains the line and column indices of the first character of bar. The output endLoc contains the line and column indices of the last character of bar.

[startLoc,endLoc]=getLineColumn(info.Functions(2))
startLoc = 

  struct with fields:

      Line: 6
    Column: 1


endLoc = 

  struct with fields:

      Line: 8
    Column: 3

Create a report information object for a code generation process that fails. You then locate the part of MATLAB code that caused an error message.

Define the MATLAB function foo:

function b = foo(a)
b = svd(a,0);
end

Generate a static C library for foo. Specify the input as a string scalar. Export the code generation report information to the variable info in your base MATLAB workspace.

codegen -config:lib foo -args {"A string scalar"} -reportinfo info

Code generation fails because a string scalar is not a valid input for the MATLAB function svd. The code generator creates a report information object info in the base MATLAB workspace.

The property info.Messages is a two-dimensional array containing descriptions of two code generation messages. Inspect the description of the first message.

info.Messages(1)
  Message with properties:

    Identifier: 'Coder:toolbox:unsupportedClass'
          Type: 'Error'
          Text: 'Function 'svd' is not defined for values of class 'string'.'
          File: [1×1 coder.CodeFile]
    StartIndex: 26
      EndIndex: 33

To manually inspect the segment of MATLAB code that caused this error message, first display the text of the file associated with this error message.

info.Messages(1).File.Text
'function b = foo(a)
 b = svd(a,0);
 end
 '

Use getLineColumn to locate the beginning and end of the part of the code that caused the error message. The output startLoc contains the line and column indices of the first character of the code segment. The output endLoc contains the line and column indices of the last character of the code segment.

[startLoc,endLoc] = getLineColumn(info.messages(1))
startLoc = 

  struct with fields:

      Line: 2
    Column: 5


endLoc = 

  struct with fields:

      Line: 2
    Column: 12
These locations correspond to the beginning and the end of the function call 'svd(a,0)' in the text of foo.m.

Input Arguments

collapse all

Object describing a MATLAB function or a method in a MATLAB class that is involved in code generation, specified as one of the following:

A coder.Message object describing an error, warning, or informational message produced during code generation from MATLAB code. See coder.Message Properties.

A coder.CallSite object describing a function call site in your MATLAB code. See coder.CallSite Properties.

Output Arguments

collapse all

Structure array with two fields: Line and Column.

  • startLoc.Line is the line index of the first character of the MATLAB code in the text of the file containing the code.

  • startLoc.Column is the column index of the first character of the MATLAB code in the text of the file containing the code.

Structure array with two fields: Line and Column.

  • endLoc.Line is the line index of the last character of the MATLAB code in the text of the file containing the code.

  • endLoc.Column is the column index of the last character of the MATLAB code in the text of the file containing the code.

Version History

Introduced in R2019a