trying to use anonymous function but it returns too many input arguments

9 ビュー (過去 30 日間)
Tunechi
Tunechi 2020 年 9 月 19 日
編集済み: Tunechi 2020 年 9 月 20 日
I separately wrote the following function
function [func] = fwet_1(~)
func = @(gm1,Sc,Scmax) (Sc/Scmax)^gm1;
end
and when I to run
fwet_1(0.23,1,3)
it returns
Error using fwet_1
Too many input arguments.
any idea?

採用された回答

Tunechi
Tunechi 2020 年 9 月 19 日
編集済み: Tunechi 2020 年 9 月 20 日
I just got answer form stackoverflow answer by @Mad Physicist
fwet_1 takes up to 1 input. Your syntax is calling fwet_1 with three inputs, not the function handle it returns.
You can call the function handle like this:
x = fwet_1
x(0.23,1,3)
If your function really does nothing but return a function handle, skip the function entirely, or remove the unnecessary level of nesting. E.g.:
function [val] = fwet_1(gm1,Sc,Scmax)
val = (Sc/Scmax)^gm1;
end
If you need a function handle, just use @fwet_1. There's no difference between a regular function and an anonymous one when you pass it around.
  2 件のコメント
Steven Lord
Steven Lord 2020 年 9 月 19 日
Technically fwet_1 accepts up to 1 input argument and if you pass something into it as an input argument it will ignore that input. If you don't pass anything into it, that's fine too.
Tunechi
Tunechi 2020 年 9 月 19 日
Thanks @Steven Lord

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by