Effacer les filtres
Effacer les filtres

Recursion for Unnesting 1x1 Cell Array

2 vues (au cours des 30 derniers jours)
Qasim Hassan
Qasim Hassan le 11 Avr 2019
Commenté : James Tursa le 11 Avr 2019
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?

Réponse acceptée

James Tursa
James Tursa le 11 Avr 2019
Modifié(e) : James Tursa le 11 Avr 2019
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 commentaires
Qasim Hassan
Qasim Hassan le 11 Avr 2019
Modifié(e) : Qasim Hassan le 11 Avr 2019
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.
James Tursa
James Tursa le 11 Avr 2019
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by