I have been getting continuous errors for reaching the maximum recursion limit when running various test cases for my function. Since unnesting a 1x1 cell array to get a string can usually be done by:
ca = {{{'math'}}}
while iscell(ca)
ca = ca{1}
end
out = ca
For recursion, I did this:
function out = doYouEverFeel(ca)
if ischar(ca)
out = ca
else
out = doYouEverFeel(ca{1});
end
end
Why is this not working?

 채택된 답변

James Tursa
James Tursa 2019년 4월 11일
편집: James Tursa 2019년 4월 11일

0 개 추천

Are you sure it is a single quote char string 'example' and not a double quote string "another_example"? What is the actual error message displayed? Your recursion code will only work if the end result is always a char variable.

댓글 수: 2

Qasim Hassan
Qasim Hassan 2019년 4월 11일
편집: Qasim Hassan 2019년 4월 11일
Yes, it is always single quote char strings.
ERROR: Maximum recursion limit of 500 reached.
EDIT: I realized something was amiss in my test case file. Code runs as expected.
Is your nested cell really more than 500 levels deep? I would rewrite your recursion function to instead simply use your while loop.
function out = doYouEverFeel(ca)
while iscell(ca)
ca = ca{1}
end
out = ca
end

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

추가 답변 (0개)

카테고리

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

태그

질문:

2019년 4월 11일

댓글:

2019년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by