I keep getting 'Undefined function or variable 't' ' after running the code

조회 수: 4 (최근 30일)
Anas Din
Anas Din 2018년 9월 29일
편집: mohammed lubbad 2018년 10월 8일
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개)

Stephen23
Stephen23 2018년 9월 29일
편집: Stephen23 2018년 9월 29일
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
Stephen23
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
Anas Din 2018년 9월 29일
@Stephen Cobeldick, thank you for your help, I will read the documentation and get back to you ASAP.
P.S Thank you for your help so far.

댓글을 달려면 로그인하십시오.


mohammed lubbad
mohammed lubbad 2018년 10월 8일
편집: mohammed lubbad 2018년 10월 8일
do you have this book anas ?? i f you need anything contact me @ lubbad1991@gmail.com i have something

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by