I am trying to call a function to calculate timber lengths however every time i do it returns the error of 'Too many output arguments'. I am running Matlab R2017a.
% Calculate total timber lengths needed
total_length = no_of_wall_studs * GET_NEAREST_LENGTH(wall_height);
total_length = total_length + (top_plates * GET_NEAREST_LENGTH(wall_width));
% Function to calculate timber length
function GET_NEAREST_LENGTH(length)
timber_length = MAX_WALL_WIDTH;
% Check is length is minimum
if length <= MIN_TIMBER_LENGTH
length = MIN_TIMBER_LENGTH;
end
% Start at 5.4 and work down to find the correct length
while length < (timber_length-INC)
timber_length = timber_length-INC;
end
end
any help is appreciated

 채택된 답변

James Tursa
James Tursa 2019년 8월 27일
편집: James Tursa 2019년 8월 27일

0 개 추천

Your function isn't coded to return anything. Try this:
function timber_length = GET_NEAREST_LENGTH(length)
Btw, "length" is the name of a built-in MATLAB function ... to avoid confusion in your code you should consider using a different name.

댓글 수: 3

Jason Froome
Jason Froome 2019년 8월 27일
편집: James Tursa 2019년 8월 27일
Thanks, that cleared the output error, however now i am getting a different error that tells me "Undefined function or variable 'MAX_WALL_WIDTH'."
even though MAX_WALL_WIDTH is defined as 5.4 at the top of my script.
I changed the name "length" as well after doing some google searches to ensure the new name wasnt a matlab function.
Also thanks for editing the question to show the code correctly
James Tursa
James Tursa 2019년 8월 27일
편집: James Tursa 2019년 8월 27일
Functions have their own workspace that generally isn't shared with the caller workspace (unless you do something to make it shared, like nested functions). So your function can't see the variables at your script level. You could simply pass them in as arguments, which is actually good code design to make your function self-contained and only dependent on input arguments. E.g.,
function timber_length = GET_NEAREST_LENGTH( length, MAX_WALL_WIDTH, INC )
Just be sure to include these arguments when calling the function.
Jason Froome
Jason Froome 2019년 8월 27일
Thanks James your a champion.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2019년 8월 27일

댓글:

2019년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by