switch flag error
이전 댓글 표시
I am trying to find out why is this an error if it came out from a book. The m-file should be saved as a function or script please see code:
function [sys,x0,str,ts] = s_anm81s(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=md1InitializeSizes;
case 2,
sys=md1Update(t,x,u);
case 3,
sys=md1Outputs(t,x,u);
case 9,
sys=md1Terminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=md1InitializeSizes()
sizes = simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=0;
sizes.NumInputs=1;
sizes.DirFeedthrough=0;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[0.25 0];
if(isempty(findobj('UserData',gcb)))
h_fig=figure('Position', [200 200 400 300],...
'MenuBar','none','NumberTitle','off',...
'Resize', 'off',...
'Name', ([gcs,'RollingDisk']));
set(h_fig,'UserData',gcb);
set(h_fig,'DoubleBuffer','on');
r=2;
R=12;
q=r;
thp=0:0.2:pi;
xp=R*cos(thp);
yp=-R*sin(thp);
xp=[xp,-R,-(R+q),-(R+q),(R+q),(R+q),R];
yp=[yp,0,0,-(R+q),-(R+q),0,0];
cl_x=[0,0];
cl_y=[0,-R];
thp=0:0.3:2.3*pi;
xd=r*cos(thp);
yd=r*sin(thp);
hd=fill(xp,yp,[0.85,0.85,0.85]);
hold on;
set(hd,'EraseMode','none');
axis('equal');axis('off');
hd0=plot(cl_x,cl_y,'k--');
set(hd0,'EraseMode','none');
theta=0;
xc=(R-r)*sin(theta);
yc=-(R-r)*cos(theta);
psi=theta*(R-r)/r;
xm_c=r*sin(psi);
ym_c=r*cos(psi);
xm=xc+xm_c;
ym=yc+ym_c;
hd2=fill(xd+xc,yd+yc,[0.85,0.85,0.85]);
hd3=plot([xc,xm],[yc,ym],'k');
set_param(gcb,'UserData',[hd2,hd3]);
end
function sys=md1Update(t,x,u)
sys=[];
r=2;
R=12;
q=r;
userdat=get_param(gcb,'UserData');
hd2=userdat(1);
hd3=userdat(2);
theta=u(1);
xc=(R-r)*sin(theta);
yc=-(R-r)*cos(theta);
psi=theta*(R-r)/r;
xm_c=r*sin(psi);
ym_c=r*cos(psi);
xm=xc+xm_c;
ym=yc+ym_c;
thp=0:0.3:2*pi;
xd=r*cos(thp);
yd=r*sin(thp);
set(hd2,'XData',xd+xc);
set(hd2,'YData',yd+yc);
set(hd3,'XData',[xc, xm]);
set(hd3,'YData',[yc,ym]);
function sys=md1Outputs(t,x,u)
sys=[];
function sys=md1Terminate(t,x,u)
sys=[];
[EDITED, 12-Dec-2011 14:01 UTC, Jan Simon]: Code formatted
댓글 수: 5
the cyclist
2011년 12월 10일
This would be astronomically easier to read if you used the "Code" button to format your post.
Walter Roberson
2011년 12월 10일
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Jan
2011년 12월 10일
Dear Gabriel: I've formatted the code for you this time. If you do not care about a proper formatting and the comments, the readers get the impression, that you do not really care about the question and do not really need an answer.
Jan
2011년 12월 10일
Which error do you get - please copy the complete message? How do you call this funtion?
muhammad ghufran
2021년 9월 29일
@Jan I am facing same issue like error come "Switch flag" how can I solve this error please guide me
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Control System Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!