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!


