フィルターのクリア

Use of handle in matlab 2013b

3 ビュー (過去 30 日間)
Muazma Ali
Muazma Ali 2019 年 1 月 25 日
回答済み: Guillaume 2019 年 1 月 25 日
hi
I am wondering whether I can use the handle function in my functions when I am using matlab 2013b.
  9 件のコメント
Muazma Ali
Muazma Ali 2019 年 1 月 25 日
well there is nothing wrong with the code I think..it was taken from a book.
Muazma Ali
Muazma Ali 2019 年 1 月 25 日
well x is the local variable in the main function that the sub function can change..then I think therefore it is no need to have this as input variable

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

回答 (3 件)

madhan ravi
madhan ravi 2019 年 1 月 25 日
編集済み: madhan ravi 2019 年 1 月 25 日
%%%% The below can be in a command window or in a separate script file
h = counter ; % function call
h(0)
% ^----- x is zero , I suggest you to keep x as input so you can change it whenver you call the function
%%%%% The below should be saved as in a separate file named counter.m since you are using 2013b
function h = counter
h=@addone;
function y=addone(x);
x=x+1;
y=x;
end
end
  3 件のコメント
madhan ravi
madhan ravi 2019 年 1 月 25 日
Muazma Ali's answer moved here:
i think u misunderstood..evry time I call h() the value will be increased by 1 and accumulated..that was what I need for my purpose..I am not interested in sending my value.
for instance the purpose if of counting the times one procedure has run
madhan ravi
madhan ravi 2019 年 1 月 25 日
編集済み: madhan ravi 2019 年 1 月 25 日
Can you show where you are going to use it to count the number of times ? if so recursive call could suffice your need.

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


Adam
Adam 2019 年 1 月 25 日
function h = counter
persistent x
if isempty( x )
x=0;
end
h=addone;
function y=addone
x=x+1;
y=x;
end
end
would work, though it is an ugly way to program such functionality in general.

Guillaume
Guillaume 2019 年 1 月 25 日
Does R2013b support function handles? Yes. Function handles have existed since before R2006a.
Irrespective of that Is it a good idea to create a function that return a self-incrementing variable on each call? Absolutely not. While it can be done using persistent variables (see adam's answer), this is antithetical of matlab design philosophy. The traditional way is to pass the variable to be incremented as input and return the incremented variable as output. Only under very rare circumstances should functions have internal state (and if so, a handle class would probably be better).

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by