Please urgent help

I am facing minor issues regarding my code. I want to have a real time simulation where an object moves according to a sine wave block fed through an S-function. The problem is that the model simulation starts alone and then the drawing starts to appear in the GUI.Also,I could not get the value of the sine wave S-function and updating it in the variable arm angle in the GUI M-file draw function. Could you please assess me on this issue. My files are:
1- GUI M-file (using guide) and it is structure is :
function varargout = auto(varargin) ................................ .
.
. .
including all the buttons callbacks and handles initialization . Start button function having the command sim() and call the draw function .
Function [] = draw(arm angle) arm angle need to have it is value from the S-function to control its movement in real time. .
.
drawing commands of the object needed
.
.
drawnow()
_________________________
2- s-function file
function [sys,x0,str,ts] = my_sfunc(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 1,
sys=mdlDerivatives(t,x,u);
case 2,
sys=mdlUpdate(t,x,u);
case 3,
sys=mdlOutputs(t,x,u);
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
case 9,
sys=mdlTerminate(t,x,u);
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=0;
sizes.NumInputs=2;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [-1 0];
function sys=mdlDerivatives(t,x,u)
sys = [];
function sys=mdlUpdate(t,x,u)
sys = [];
function [sys,x0,str,ts]=mdlOutputs(t,x,u)
sys = [];
function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; sys = t + sampleTime;
function sys=mdlTerminate(t,x,u)
sys = [];
________________________________________________________
3- simulink model having a sine wave block and a clock connected to the input of the s function block u(1), u(2)

1 Comment

Still waiting for your answer in your previous thread on this topic,
http://www.mathworks.com/matlabcentral/answers/26785-running-simultaneously-a-gui-file-and-simulink-model-with-s-function

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 23 Jan 2012

1 vote

Your mdlUpdate() function does not appear to be invoking your drawing routine.
Also, make sure you have set the direct feedthrough flags appropriately; see http://www.mathworks.com/matlabcentral/newsreader/view_thread/125257

4 Comments

koko kinder
koko kinder on 23 Jan 2012
Thanks Walter, I really appreciate your help. I am a beginner and I don't know that much. My drawnow()is at the end of the draw function in the M-file and it is working fine without using the sim(). Would you please tell me more about how to invoke my drawing routine from the mdlUpdate.Thanks again
koko kinder
koko kinder on 23 Jan 2012
Dear Walter, so far, I have managed to have all the codes executed because I have changed the simulation time from infinity to real value but still they are operating one after another. Thus, I get the simulation model running and after it has stopped I see the drawing appears. Regarding the break points I really do not know where to use it and how. Thanks
mdlUpdate() should call draw() to pass in the current arm angle. Or perhaps it would be easier to have mdlOutputs() call it. I find the difference to be confusing, especially when I start reading about feedthrough.
koko kinder
koko kinder on 24 Jan 2012
Sorry about any inconvenience with the questions.The questions are on the same code. I've tried to use different types of breakpoints for example I've used the dbcont after sim() and it has the same effect with no progress. Thanks

Sign in to comment.

More Answers (1)

TAB
TAB on 24 Jan 2012
A more convinient way to access runtime simulink data by using Simulink.RunTimeBlock object or by using event listener callback(add_exec_event_listener).
To see a good demo type
>> sldemo_msfcn_lms
on command window
See

Community Treasure Hunt

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

Start Hunting!