How to automatically put the asked values in function by script?

2 ビュー (過去 30 日間)
Dayalram Dayalram
Dayalram Dayalram 2019 年 9 月 23 日
編集済み: Dayalram Dayalram 2019 年 9 月 26 日
I have a function
function result=add()
a=input('what is the first number:');
b=input('what is the second number:');
result=a+b;
Now I want to create a separate script that puts the asked values 'a' and 'b' automatically in function.
I want to add 3 and 5. I tried this but failed.
add;
3;
5;
Does someone has a solution?

回答 (1 件)

Adam
Adam 2019 年 9 月 23 日
編集済み: Adam 2019 年 9 月 23 日
You would have to define a function that takes two arguments (or an array of inputs to add), e.g. for an example of a fixed two arguments:
function result = add( a, b )
if ~exist( 'a', 'var' )
a=input('what is the first number:');
end
if ~exist( 'b', 'var' )
b=input('what is the second number:');
end
result = a + b;
Then you can call it as any of the following
add
add( 3 )
add( 3, 5 )
Also don't use 'sum' as a variable name, it is a function name and shouldn't be overwritten even if you are using your add function rather than the builtin sum function.
  1 件のコメント
Dayalram Dayalram
Dayalram Dayalram 2019 年 9 月 26 日
I just want to create a saperate script from function, that automatically put the asked values in variables one after another. I don't want to call a function by inputs as you written.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by