getLineColumn - Find locations of beginning and end of MATLAB code involved in code generation - MATLAB (original) (raw)
Main Content
Find locations of beginning and end of MATLAB code involved in code generation
Syntax
Description
[[startLoc](#mw%5F37235a0e-f4a0-46bd-a024-9d4cc1950bd1),[endLoc](#mw%5F4c05990f-d47c-47b7-9fd9-0e67e92f7bfc)] = getLineColumn([obj](#mw%5Fb93f4b63-af38-4f48-96d3-9459a6c4c259))
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.
[[startLoc](#mw%5F37235a0e-f4a0-46bd-a024-9d4cc1950bd1),[endLoc](#mw%5F4c05990f-d47c-47b7-9fd9-0e67e92f7bfc)] = getLineColumn([obj_message](#mw%5F30040fff-004b-42df-bb7d-1211aa43a064))
returns the line and column indices of the first and last character of the MATLAB code that caused the code generation message described byobj_message
.
[[startLoc](#mw%5F37235a0e-f4a0-46bd-a024-9d4cc1950bd1),[endLoc](#mw%5F4c05990f-d47c-47b7-9fd9-0e67e92f7bfc)] = getLineColumn([obj_callsite](#mw%5Fe67f8dc9-9df4-409b-9de7-1743234be911))
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
Locate a MATLAB Function in File
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 variableinfo
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 containingbar
.
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 functionbar
in this text. The output startLoc
contains the line and column indices of the first character of bar
. The outputendLoc
contains the line and column indices of the last character ofbar
.
[startLoc,endLoc]=getLineColumn(info.Functions(2))
startLoc =
struct with fields:
Line: 6
Column: 1
endLoc =
struct with fields:
Line: 8
Column: 3
Locate MATLAB Code That Causes a Code Generation Error Message
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 variableinfo
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.
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 outputendLoc
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
obj
— Object describing MATLAB function or method involved in code generation
handle object
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.Function
object for the description of a function. See coder.Function Properties. - A
coder.Method
object for the description of a method. Seecoder.Method Properties.
obj_message
— Object describing code generation error message
coder.Message
object
A coder.Message
object describing an error, warning, or informational message produced during code generation from MATLAB code. See coder.Message Properties.
obj_callsite
— Object describing function call site
coder.CallSite
object
A coder.CallSite
object describing a function call site in your MATLAB code. See coder.CallSite Properties.
Output Arguments
startLoc
— Line and column indices of the first character of MATLAB code
structure array
Structure array with two fields: Line
andColumn
.
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.
endLoc
— Line and column indices of the last character of MATLAB code
structure array
Structure array with two fields: Line
andColumn
.
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