필터 지우기
필터 지우기

Problem with custom function

조회 수: 1 (최근 30일)
Pietro Fiondella
Pietro Fiondella 2022년 7월 4일
편집: Siraj 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
disp("Bad sizing")
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
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'.
So assign in the following way:
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
Siraj 2022년 7월 4일
편집: 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
disp("Bad sizing");
BESSsize = 0;
end
end
Hope it helps.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by