Nesting function trouble with undefined variable

Trying to code a simple nesting function and I can't understand why I get an undefined function or variable warning for 'a'
function [Y] = nesty(d,e)
Y(d,e)=2*a;
nestmini
function [a] = nestmini(d,e)
a=d+e;
end
end

 채택된 답변

Walter Roberson
Walter Roberson 2017년 11월 19일
function [Y] = nesty(d,e)
a = nestmini;
Y(d,e)=2*a;
function [a] = nestmini(d,e)
a=d+e;
end
end
Remember, the variable named as output in a function is a dummy variable that is not assigned outside the function

댓글 수: 5

aldburg
aldburg 2017년 11월 19일
편집: aldburg 2017년 11월 19일
Thank you, I apologize if my questions seem trivial.
I tried running the code and I got an error
Not enough input arguments.
Error in nesty/nestmini (line 5)
a=d+e;
Error in nesty (line 2)
a = nestmini;
Stephen23
Stephen23 2017년 11월 19일
편집: Stephen23 2017년 11월 19일
@aldburg: KL gave you the correct solution already. The above answer is buggy, as it omits to call the nested function with the input arguments that you have defined it with:
function Y = nesty(d,e)
a = nestmini(d,e);
Y(d,e) = 2*a;
function a = nestmini(d,e)
a = d+e;
end
end
Although in that case there is absolutely no point in having written nestmini as a nested function, and nestmini could just be a local function. Perhaps you meant to pass d and e via the common workspace rather than as input arguments, which really makes use of nestmini being a nested function:
function Y = nesty(d,e)
a = nestmini();
Y(d,e) = 2*a;
function a = nestmini()
a = d+e;
end
end
Both of these work: I tested them.
I slightly modified the code and I got it to work. Quick question, when my code is Y(d,e) I get an answer that is 2X1 and when I simply use Y=... I get a 1X1 answer. What gives?
function [Y] = nesty(d,e)
Y=2*nestmini(d,e);
function [a] = nestmini(d,e)
a=d+e;
end
end
Stephen23
Stephen23 2017년 11월 19일
편집: Stephen23 2017년 11월 19일
@aldburg:
  • Y=... defines a totally new variable with the name Y. It has the size of whatever the RHS has.
  • Y(d,e)=... uses indexing to allocate values into the selected elements of Y. If Y does not exist before that indexing then MATLAB creates Y and fills the unindexed elements with zero.
The introductory tutorials teach these kind of basic MATLAB concepts, such as defining variables and using indexing, and are highly recommended for all beginners:
Thanks for explaining that. I will start that once I finish my courses for the quarter. I am learning Matlab to help with my composite mechanics homework.

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

추가 답변 (1개)

KL
KL 2017년 11월 19일
You're passing d,|e| to nesty function but you're using a on the right hand side of the equation. Do not expect matlab to call the nestmini function for you.
If you want to use the function return directly, try something like,
Y(d,e) = 2*nestmini(d,e);

카테고리

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

태그

질문:

2017년 11월 19일

편집:

2017년 11월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by