is there any way to pause the code if there exist an imaginary variable?

19 ビュー (過去 30 日間)
hosein bashi
hosein bashi 2022 年 1 月 3 日
コメント済み: dpb 2022 年 1 月 4 日
I want matlab to stop if an imaginary number produced any where in the code. I can make an if condition for specific variable but is it possible to make code stop on every where imaginary number is made?
  1 件のコメント
Adam Danz
Adam Danz 2022 年 1 月 3 日
Could you elaborate on the larger goal? It sounds like we may be sneaking into an XY problem.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 1 月 3 日
At any particular location, you can insert a conditional breakpoint with the condition
any([whos().complex])
However, it is not possible to put in a breakpoint that stops as soon as any variable is assigned a complex value, and it is not possible to put in a breakpoint that stops as soon as any expression is assigned a complex value.
There is no call such as dbstop if complex

その他の回答 (2 件)

dpb
dpb 2022 年 1 月 3 日
編集済み: dpb 2022 年 1 月 3 日
While I've not tried it, the following should work if you can arrange the conditions needed. The first one may be the kicker in that the variable w will only know of those variables that exist in the workspace when it is executed...any that come later won't be known.
Perhaps preallocation of everybody would serve -- something like having an
IMPLICIT NONE
statement in a Fortran code module; require a declaration for everything of interest. One would presume things like loop indices, etc., would not be of interest/need to be covered.
w=whos; % create the variable in your function--it will have to execute after all variables exist
Then set the expression as
dbstop in file if any([w.complex])
The real kicker is, of course, that you have to have the variable w recreated continuously -- it won't do for it to be static.
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 1 月 3 日
dbstop in FUNCTIONNAME if any([whos().complex])
or
dbstop in FILENAME if any([whos().complex])
both have the effect of putting the condition on the first executable line of the function.
dpb
dpb 2022 年 1 月 4 日
Good catch, Walter...shoulda' thunk of it...

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


Image Analyst
Image Analyst 2022 年 1 月 3 日
I don't think anywhere within the program, but you might be able to check certain specific variables at certain specific locations in the code, like
squareRoot = sqrt(someNumber); % Some operation that might produce a complex number.
% See if it's complex.
if imag(squareRoot) ~= 0
% If the imaginary part is not zero, then it's complex, and we need to warn the user.
uiwait(warndlg('Hey!!! The "squareRoot" variable is complex!!!'));
end

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by