Is there a way of identifying the number of the line of code where one outputs (e.g. fprintf) a message?
For example, suppose I want to identify where in my code a particular "break" statement is executed:
fprintf('\t\t(2000) function_X:\t**** Break out ****\n');
I might have other such statements:
fprintf('\t\t(2903) function_X:\t**** Break out ****\n');
Can I automate the line nos. "2000" & "2903" to something dynamic such as:
fprintf('\t\t(<line no>) function_F:\t**** Break out ****\n');
This would allow me to enter or delete lines of code without having to search for and bring up-to-date the line nos. in the fprintf statements.

1 件のコメント

Marcel Kreuzberg
Marcel Kreuzberg 2022 年 4 月 7 日
maybe it is possible using matlab commands: error or warning

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2022 年 4 月 7 日
編集済み: Stephen23 2022 年 4 月 7 日

0 投票

mytest()
9 hello 11 world
function mytest()
%
A = 1;
B = 2;
%
F = @(s)s(1); % helper function
%
fprintf('%3u %s\n',F(dbstack()).line, 'hello')
%
fprintf('%3u %s\n',F(dbstack()).line, 'world')
end
Note that in this case the file line numbers also includes the line where the function is called: this is because this forum creates a script from all of the code and runs that. The line numbers are for the entire file (script, in the case of this forum).
Note that runt-ime code introspection (such as calling DBSTACK) is slow: if you are just doing this a few times that might be acceptable. See: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html#buwj1nn

1 件のコメント

Tyto
Tyto 2022 年 4 月 7 日
Happiness!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2022 年 4 月 7 日

コメント済み:

2022 年 4 月 7 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by