output arguments not assigned

Hello,
When I run this function
function [outcome] = days_of_the_week(day)
end
Output argument "outcome" (and maybe others) not assigned
Matlab says outcome argument not assigned (but still works despite this error)
BUT when I change [outcome] to [day]
function [day] = days_of_the_week(day)
end
The error goes away and the function works
Why?
Am I missing something obvious? Apologies, I am new to Matlab. Thank you.

回答 (1 件)

David Hill
David Hill 2019 年 10 月 22 日

0 投票

Because 'day' is the input variable, it is present in the function and will be passed to the output. the 'outcome' variable is never assigned in the function, so the error tells you that and nothing gets passed to the output. The error does not cause the function, which does nothing, not to work.

4 件のコメント

Sarah K
Sarah K 2019 年 10 月 22 日
編集済み: Sarah K 2019 年 10 月 22 日
So is 'day' the correct term to use, not outcome?
or should I assign outcome = day on the 2nd line?
thanks!
David Hill
David Hill 2019 年 10 月 22 日
Either is fine, but what is the purpose of your function?
Sarah K
Sarah K 2019 年 10 月 22 日
so the name assigned to the [outcome] should always be the same as the name assigned to the (input), for basic purposes? Thanks.
function [outcome] = name_of_function(input)
end
David Hill
David Hill 2019 年 10 月 22 日
No, the output is generally a different named variable.
function [outcome] = name_of_function(input)
outcome=2*input;
end

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

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2019 年 10 月 22 日

コメント済み:

2019 年 10 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by