Function definition not supported in this context. Create functions in code file.
이전 댓글 표시
Below is my code to solve the parallel plate capacitor problem.
clear all
x=linspace(1,20,10);
y=linspace(1,20,10);
[X,Y]=meshgrid(x,y);
C=delaunay(X(:),Y(:));
xy=[X(:) Y(:)];
Np=length(xy);
S=MatGlob(xy,C);
F=sparse(Np,1);
V0=1;
G=1e12;
for k=1:Np;
if x(k)==1
S(k,k)=S(k,k)+G;
F(k)=G*(-V0);
if x(k)==20
S(k,k)=S(k,k)+G;
F(k)=G*(+V0);
end
V=S\F;
figure
plot(xy,V,'*-')
title('potential(V)')
xlabel('postition(m)')
ylabel('postition(m)')
hold on
function S=MatGlob(xy,C)
Np=length(xy);
S=sparse(Np,Np);
Epsr=3.5;
Ne=size(C,1);
for k=1:Ne
N=C(k,:);
x1=xy(N(1),1);
x2=xy(N(2),1);
x3=xy(N(3),1);
y1=xy(N(1),2);
y2=xy(N(2),2);
y3=xy(N(3),2);
abc1=[1 x1 y1;1 x2 y2;1 x3 y3]\[1;0;0];
abc2=[1 x1 y1;1 x2 y2;1 x3 y3]\[0;1;0];
abc3=[1 x1 y1;1 x2 y2;1 x3 y3]\[0;0;1];
Grad=[abc1(2) abc2(2) abc3(2); abc1(3) abc2(3) abc3(3)];
Se=Epsr*(Grad'*Grad)*abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))/2;
S(N,N)=S(N,N)+Se;
end
end
Yet, Matlab annouce the error as follows: "Function definition not supported in this context. Create functions in code file" Would you please help me to fix this error? Thank you in advance.
댓글 수: 5
Adam
2018년 10월 11일
What version of Matlab are you using?
Simplest option is to do exactly what the error suggests though - create a new .m file, put the function in there and add it to your path.
@Duc Tu Vu: your code is badly aligned. Badly aligned code is one way that beginners hide bugs in their code, just like you have done. You should align your code using the default MATLAB Editor settings. You can also align existing code: select the code and press ctrl+i.
Mebo Valle
2021년 12월 8일
I am experiencing the same problem, i have tried to align but nothing is happening, its just a simple and small code
function myRand
a=1+rand(3,4)*9
end
using R2019a
Mebo Valle
2021년 12월 8일

Adam
2021년 12월 8일
And you are getting the error about function definitions not being permitted in that context or some other problem?
채택된 답변
추가 답변 (1개)
Duc Tu Vu
2018년 10월 11일
0 개 추천
댓글 수: 5
WHISKY Chen
2019년 3월 23일
How did you solve that problem? Could you please help me!
I have the same problem and... haven't solved it.
egg
2019년 7월 16일
same here!
Steven Lord
2019년 7월 16일
Try smart indenting your code to determine if you're trying to define a nested function inside an if, for, while, etc. statement (which is not allowed.)
Also make sure you're using release R2016b or later. The ability to define functions in scripts was introduced in release R2016b and will not work in earlier releases.
Naireeta Deb
2020년 2월 27일
It doesn't work :/
Steven Lord
2020년 2월 27일
Without more information about what you're doing (and ideally a small example with which you can reproduce the error) it's unlikely we'll be able to give you a more specific suggestion than what I did in my comment immediately before yours.
Make sure you're using release R2016b or later and make sure you're not trying to define the function inside an if, for, while, etc. statement (and you're not trying to define it by typing "function myoutput = ..." at the prompt in the Command Window.)
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!