Why does this not work (while Solution 1328031 does work)?
Because a comma separated list cannot be assigned to a single variable, this is how your previous solution catched the assignment error and triggered the catch statement (as desired). However, what you did here is only to generate a comma separated list without any assignment, and thus, no assignment error occurs which cannot trigger the catch statement.
Thanks, Peng Liu, for your concise explanation! It greatly helps in improving my understanding of cell variables/commaseparated lists as well as the trycatch function and error handling, which are topics that I still find quite complicated; but your comment explains it very clearly and easily! :)
However, on closer inspection, I feel there's still something missing in the explanation. If it's merely a matter of failed/absent variable assignment to a commaseparated list, then why does this code still successfully enter into the catch statement in tests #3 and #6 (in which input x is a noninteger number)?
For the noninteger test cases #3 and #6, it is the subscript indexing error that was catched in your ans{index} indexing statement (because subscript index has to be either positive integer or logical).
But in tests #7 and #8 too, the subscript index is neither a positive integer nor a logical value; so why does it not lead to the same subscript indexing error that invokes the 'catch' statement?
Tests #7 and 8 involved an 0*0 array [] as the index, which is an extreme case where MATLAB returns nothing without issuing any error. Since no error occurs in the try block, the program flow does not enter the catch block. Hope this helps.
Thanks, Peng Liu! I would have expected that [] as an index of a cell variable would lead to an error, but apparently MATLAB accepts it and produces a commaseparated list of zero terms (which I now realize is apparently different from an empty matrix, otherwise it would have assigned "ans = [];"). Anyway, again, thanks for your comments, it really helped me understanding MATLAB better!
Test  Status  Code Input and Output 

1  Pass 
x = 1;
n_correct = 'odd';
assert(isequal(oddornot(x),n_correct))
ans =
'odd'

2  Pass 
x = 12;
n_correct = 'even';
assert(isequal(oddornot(x),n_correct))
ans =
'even'

3  Pass 
x = 467.3;
n_correct = 'error';
assert(isequal(oddornot(x),n_correct))
ans =
'error'

4  Pass 
x = 911;
n_correct = 'odd';
assert(isequal(oddornot(x),n_correct))
ans =
'odd'

5  Pass 
x = 8230749726348;
n_correct = 'even';
assert(isequal(oddornot(x),n_correct))
ans =
'even'

6  Pass 
x = pi;
n_correct = 'error';
assert(isequal(oddornot(x),n_correct))
ans =
'error'

7  Fail 
x = 'a';
n_correct = 'error';
assert(isequal(oddornot(x),n_correct))

8  Fail 
x = 'donald';
n_correct = 'error';
assert(isequal(oddornot(x),n_correct))

Project Euler: Problem 6, Natural numbers, squares and sums.
965 Solvers
330 Solvers
Find the square of the sum of the digits of a number
114 Solvers
383 Solvers
177 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!