How do you call a nested function?
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello. I understand how a nested function works, but I'm having a problem calling it in the command window. I don't know if I'm calling it wrong or not.
댓글 수: 0
채택된 답변
Wayne King
2013년 6월 18일
편집: Wayne King
2013년 6월 18일
You want to put the nested function inside another function (as the term nested implies) and then call the top-level function. As an example, create the following file, evenodd.m
function [even, odd] = evenodd(x)
even = evensamples(x);
odd = oddsamples(x);
function even = evensamples(x)
even = x(1:2:end);
end
function odd = oddsamples(x)
odd = x(2:2:end);
end
end
Save the above file evenodd.m in a folder on the MATLAB path and then call
>> x = randn(16,1);
>> [even,odd] = evenodd(x);
Note that evenodd() calls two nested functions evensamples() and oddsamples()
댓글 수: 0
추가 답변 (1개)
Jan
2013년 6월 18일
You cannot call a nested function from the outside, e.g. from the command line, directly. This is the nature of a nested function. Sub-functions cannot be called directly also.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!