I keep getting 'Undefined function or variable 't' ' after running the code
이전 댓글 표시
I am following an example in a MATLAB sliding mode control book, however the book does not do a good job of explaining how to follow the examples. the code from the book is:
function [sys,x0,str,ts]=spacemodel(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
sys=mdlDerivatives(t,x,u);
case 3
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled Flag =',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=1;
sizes.NumInputs=3;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=0;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[];
function sys=mdlOutputs(t,x,u)
J=2;
thd=u(1);
th=u(2);
dth=u(3);
e=th-thd;
de=dth;
c=10;
s=c*e + de;
xite=1.1;
k=0;
%k=10;
ut=J*(-c*dth-1/J*(k*s+xite*sign(s)));
sys(1)=ut;
This part 1 of 3 .m scripts for this example. When I run the .m file I get the 'Undefined function or variable 't' ' message.
Also there is a SIMULINK model in the book that I am also trying to get working.
답변 (2개)
You do not explain how you are calling the function, but that is likely to be the problem. Perhaps you are trying to call it by copy-and-pasting the function definition and trying to run that?:
[sys,x0,str,ts] = spacemodel(t,x,u,flag)
The function requires four input arguments: you cannot just "run" it (e.g. by clicking the big green "Run" button) or "run" the file, you have to call it with all four input arguments, which have to be variables that exist in the workspace, e.g.:
A = 2;
B = 2;
C = 3;
function [sys,x0,str,ts] = spacemodel(A,B,C,1)
All well-written code also includes a help section at the beginning which explains what the code does, and specifies the input and output arguments. Note that this code will throw errors for some different flag input values when not all of the output arguments are defined... this is not very well written code.
댓글 수: 5
Anas Din
2018년 9월 29일
@Anas Din: aaah, now it is clear. You are using a version of MATLAB which allows functions to be defined within scripts. But the person who (badly) wrote that code did not put end at the end of each function, which is required when defining a function within a script, like you are doing (and I would highly recommend doing all the time anyway). So you need to append end to each function properly, like this:
function ...
...
end % <- Every FUNCTION needs a matching END
function ...
...
end % etc.
Add all of those end's for every function, and try it again.
PS: do not get tricked by the end within the first function: that belongs to switch and not to the function definition, so you should have this:
function [sys,x0,str,ts]=spacemodel(t,x,u,flag)
switch flag
...
end
end
Anas Din
2018년 9월 29일
Stephen23
2018년 9월 29일
@Anas Din: Read my answer again. You call the function from the command window like this:

But where have you defined t, x, u and flag ? What values do you expect them to have, if you have not defined them?
If you are expecting to use this function with Simulink, then you should read the documentation about how to call function from Simulink. While trying things and experimenting is a great idea, it is no replacement for actually reading the documentation:
Anas Din
2018년 9월 29일
mohammed lubbad
2018년 10월 8일
편집: mohammed lubbad
2018년 10월 8일
0 개 추천
do you have this book anas ?? i f you need anything contact me @ lubbad1991@gmail.com i have something
카테고리
도움말 센터 및 File Exchange에서 Model Setup에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


