# Problem with custom function

1 回表示 (過去 30 日間)
Pietro Fiondella 2022 年 7 月 4 日

I have write this function
function BESSsize = Bsize(x,y,z)
if 0.8*z >=x+y
Bsize=(x+y)*0.8/365
elseif 0.5*x+y<= z <0.8*x+y
Bsize=z/365
else
end
It dosent run here but I saved in matlab and it works in my scripts.My problem is that if i try for example to multiply and use the result for example A=Bsize(10,50,100)*365 i obtain this error:
>> Bsize(10,50,100)
Bsize =
0.1315
>> A=Bsize(10,50,100)*365
Bsize =
0.1315
Output argument "BESSsize" (and possibly others) not assigned a value in the execution with "Bsize" function.
How can i solve this?

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

### 採用された回答

Raghav 2022 年 7 月 4 日
In the function definition:
function BESSsize = Bsize(x,y,z)
'BESSsize' is the variable which will be returned after function execution and the 'Bsize' is the function name.
You are assigning return value to 'Bsize', instead assign them to the returning variable 'BESSsize'.
BESSsize=(x+y)*0.8/365;
and
BESSsize=z/365;
Also use ; if you do not want an output from statement in which you are assigning values.
One more thing is you are using
0.5*x+y<= z <0.8*x+y
which is of the form (a<=b)<c
I think it would be better to write in the form (a<=b) && (b<c) like:
((0.5*x+y)<=z) && (z<(0.8*x+y))

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

### その他の回答 (1 件)

Siraj 2022 年 7 月 4 日

Hi,
It is my understanding that you are confusing between the function name “Bsize” and the variable “BESSsize” which is to be returned by the function.
In the if and elseif part you are creating a variable “Bsize” with the same name of the function and the output variable that you declared to be “BESSsize” is not being assigned any value. Therefore, you are seeing the error.
The solution is to change the “Bsize” to “BESSsize” in the "if" and "else if" part and also assign a value to "BESSsize" for the else part.
Refer to the documentation for more on functions in MATLAB.
A=Bsize(10,50,100)*365
A = 48.0000
function BESSsize = Bsize(x,y,z)
if 0.8*z >=x+y
BESSsize = (x+y)*0.8/365;
elseif 0.5*x+y<= z <0.8*x+y
BESSsize = z/365;
else
BESSsize = 0;
end
end
Hope it helps.

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

### カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by