Is there any known eason why my level 2 matlab S-function runs slow

1 visualizzazione (ultimi 30 giorni)
Please take a look at the following very simple level 2 matlab S-function, do you please know any reason why it should run slow.
By the way How do I past the matlab code in this textbox so that it can be readable?
/////////////////////////////////////////////////////////////
function handFunctionSeq(block)
setup(block);
function setup(block)
% Register number of ports
block.NumInputPorts = 1;
block.NumOutputPorts = 4;
% Setup port properties to be inherited or dynamic
% % Override input port properties
block.InputPort(1).Dimensions = 1;
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
block.InputPort(1).DirectFeedthrough = true;
%
% % Override output port properties
%opeing of hand: thumb
block.OutputPort(1).Dimensions = 1;
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
block.OutputPort(1).SamplingMode = 'sample';
%opening of hand: fingers
block.OutputPort(2).Dimensions = 1;
block.OutputPort(2).DatatypeID = 0; % double
block.OutputPort(2).Complexity = 'Real';
block.OutputPort(2).SamplingMode = 'sample';
%closing if hands: thumb
block.OutputPort(3).Dimensions = 1;
block.OutputPort(3).DatatypeID = 0; % double
block.OutputPort(3).Complexity = 'Real';
block.OutputPort(3).SamplingMode = 'sample';
%closing of hand: fingers
block.OutputPort(4).Dimensions = 1;
block.OutputPort(4).DatatypeID = 0; % double
block.OutputPort(4).Complexity = 'Real';
block.OutputPort(4).SamplingMode = 'sample';
% Register parameters
block.NumDialogPrms = 4;
block.SampleTimes = [-1 0];
block.SimStateCompliance = 'DefaultSimState';
%%Register methods
block.RegBlockMethod('PostPropagationSetup',@DoPostPropSetup);
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Update', @Update);
block.RegBlockMethod('Start', @Start);
block.RegBlockMethod('SetInputPortSamplingMode',@SetInputPortSamplingMode);
%end setup
function DoPostPropSetup(block)
block.NumDworks = 1;
block.Dwork(1).Name = 'x1';
block.Dwork(1).Dimensions = 1;
block.Dwork(1).DatatypeID = 0; % double
block.Dwork(1).Complexity = 'Real'; % real
block.Dwork(1).UsedAsDiscState = true;
function Start(block)
block.Dwork(1).Data =0;
%endfunction
function Outputs(block)
%delays in samples
delay_close_open=block.DialogPrm(1).Data;%e.g=256*3 samples
delay_thumb_close=block.DialogPrm(2).Data;%e.g=70 samples
delay_fingers_open=block.DialogPrm(3).Data;%e.g=70 samples
trialLength=block.DialogPrm(4).Data;%e.g=256*5 samples
block.OutputPort(1).Data=0;
block.OutputPort(2).Data=0;
block.OutputPort(3).Data=0;
block.OutputPort(4).Data=0;
%opening of hands
block.OutputPort(1).Data = block.InputPort(1).Data;%thumb signal sent first
if(block.Dwork(1).Data>=delay_fingers_open)
block.OutputPort(2).Data =block.InputPort(1).Data;%delayed fingers signal follows
else
block.OutputPort(2).Data=0;
end
%closing of hand
if(block.Dwork(1).Data>=delay_close_open)
if(block.Dwork(1).Data<=trialLength)%delaying closing of hand in samples
% disable opeinning of hand when closing hands
block.OutputPort(1).Data=0;
block.OutputPort(2).Data=0;
%initiate closing of hand starting with the fingers
block.OutputPort(4).Data = block.InputPort(1).Data;%fingers signal sent first
if(block.Dwork(1).Data>=delay_thumb_close+delay_close_open)
block.OutputPort(3).Data =block.InputPort(1).Data;%thumb signal comes after
else
block.OutputPort(3).Data=0;
end
else
%i.e the going back to opeing of hands. This is the end of the
%circle so we can go back to hand opening and reseting our work
%vectors
block.Dwork(1).Data=0;
block.OutputPort(2).Data=0;%avoid the spike of channel 2
end
else
end
%end Outputs
function Update(block)
block.Dwork(1).Data =block.Dwork(1).Data+1 ;
%end Update
function SetInputPortSamplingMode(s, port, mode)
s.InputPort(port).SamplingMode = mode;
///////////////////////////////////////////////////////////////

Risposta accettata

Kaustubha Govind
Kaustubha Govind il 17 Lug 2012
Your code looks fairly simple, and I don't see anything that stands out, but how do you figure out that it's the S-function that slows down your model? Did you use Simulink Profiler. Note that MATLAB S-functions are executed by using the MATLAB interpreter, which will in general be slower than running other Simulink blocks (which are typically implemented in C/C++). If performance is of utmost importance, and you have narrowed down the performance issue to your S-function, you may want to consider either implementing your S-function in C as a C-MEX S-function, or using an (Embedded) MATLAB Function block. Note that (Embedded) MATLAB Function blocks do not have an Update method, but unless you have a feedback loop involving your S-function, your Update method looks simple enough that you can just add it to the bottom of your Output function.
  3 Commenti
Kaustubha Govind
Kaustubha Govind il 17 Lug 2012
Modificato: Kaustubha Govind il 17 Lug 2012
Bethel: Embedded MATLAB Function blocks generate C code for execution, so they are as efficient as C-MEX S-functions. They don't have dwork vectors, but you can work around that by using persistent variables. Please see Using Persistent Variables to Model State.
bethel o
bethel o il 17 Lug 2012
Thank you very much. I will try Embedded MATLAB Function blocks.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by