General questions about function and if command.
1 回表示 (過去 30 日間)
古いコメントを表示
1. If I have a variable x, and I use a nested function f and in f.m, there is also a variable x but not equal to the first x. Will it bother the program? The x's are defined in different functions, will matlab recognize these?
2. I have an error message: --- Operands to the and && operators must be convertible to logical scalar values.
Error in V_F (line 16) if P(n)>0 && x>n-1 ---
However, P(n) is scalar and x is also scalar. What is wrong here?
Thanks.
0 件のコメント
回答 (2 件)
Matt Fig
2012 年 10 月 23 日
編集済み: Matt Fig
2012 年 10 月 23 日
For the first question, it makes a difference how and where you are accessing the variables. Post a simple example for anlaysis. For the second question, are you sure P is not a cell array and P(n) is not nan? Are you sure n is a scalar and not empty or nan? Are you sure x is a scalar and not empty or nan?
Walter Roberson
2012 年 10 月 23 日
There might perhaps be a code path in which your local "x" does not get initialized in the subfunction on and so inherits the scalar value.
3 件のコメント
Walter Roberson
2012 年 10 月 23 日
Suppose you have
if a > 3
x = 15;
elseif a < 3
x = 22;
end
then it might at first look like x is always being initialized, but in fact in the case that a is exactly 3, there would be a path in which x was not set, and so in that case would inherit its value from the nesting function.
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!