Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.(clarification and application?)
4 views (last 30 days)
Show older comments
Hi! I've seen this question asked before but have been unable to properly understand the solution. I am getting the error message "Function definition are not supported in this context. Functions can only be created as local or nested functions in code files." when running this code:
status = function celldestiny2(A,i,j)
n = numneighbors(A,i,j);
disp('n e')
disp(num2str(n))
%mindre än 2 ensam o dör, alltså blir 0
if n < 2
status=dod
else if n == 2
%om död 2 grannar -> inget händer
if A(i,j)==0
status=dod
%om levande o 2 grannar -> inget händer
else
status=levande
end
else if n == 3
%om 3 grannar och TOM plats, föds ny.
if A(i,j)==0
status=levande
%om 3 grannar och lever -> inget händer.
else
status=levande
end
else if n > 3
status=dod
disp('svalt')
%mer än 3 grannar -> dör
end
end
end
end
A
end
Things I have tried to no success:
- deleting the A at the end
- checking if I'm missing any end statements (I shouldn't be, though I'm not sure if those at the end are correctly placed)
- changing the "status" from a word output to a value (0 or 1)
I've heard something about the function definition needing to be at the "end" of the file, but what does that mean and how do I apply it practically? Any help would be greatly appreciated :)
2 Comments
Dyuman Joshi
on 2 Oct 2023
That is not how functions are defined or called. The first line of your code does not follow the correct syntax.
Answers (1)
Image Analyst
on 2 Oct 2023
Try this as the first line to declare the function:
function status = celldestiny2(A,i,j)
To learn other fundamental concepts, invest 2 hours of your time here:
1 Comment
Image Analyst
on 5 Oct 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!