- https://www.mathworks.com/help/simulink/sfg/working-with-block-data.html
- https://www.mathworks.com/help/simulink/sfg/using-s-functions-to-model-algorithms.html
- https://www.mathworks.com/help/simulink/sfg/setting-block-sample-times.html
Problems with "For loop" used inside my s-function
6 views (last 30 days)
Show older comments
I want to implement a metaheuristic algorithm (like genetic or PSO) in a s-function. I have a population of individuals, each individual are output to be evaluated by another block. but the " for loop " is faster and terminate all the loop in a time inferior to simulation step. and the output of s-function is always egal to the last individual. please need help for synchronize my for loop with the step simulation this is a simple code for explain my problem
if true
functionsampleHitOutput(block)
setup(block);
function setup(block)
%%Register number of input and output ports
block.NumInputPorts = 0;
block.NumOutputPorts = 2;
%% Setup functional port properties to dynamic block.SetPreCompOutPortInfoToDynamic; block.OutputPort(1).Dimensions = 1; block.OutputPort(2).Dimensions = 1;
block.OutputPort(1).SamplingMode = 'Sample'; block.OutputPort(2).SamplingMode = 'Sample';
% Register sample times block.OutputPort(1).SampleTime = [0 0]; block.OutputPort(2).SampleTime = [0.3 0];
%% Register methods block.RegBlockMethod('InitializeConditions', @InitConditions); block.RegBlockMethod('SetInputPortSampleTime', @InputSamplingTime); block.RegBlockMethod('SetOutputPortSampleTime', @OutputSamplingTime); block.RegBlockMethod('SetInputPortSamplingMode', @InputSamplingMode); block.RegBlockMethod('Outputs', @Output); block.RegBlockMethod('Update', @Update); block.RegBlockMethod('Terminate', @Terminate); %endfunction Setup
functionOutputSamplingTime(block) %endfunction Output Sampling Time functionInputSamplingMode(block) %endfunction Input Sampling Mode
%-------------------------------------------------------------------- % Init conditions %-------------------------------------------------------------------- functionInitConditions(block)
j=1; global out1; % Simply output this global value at Output1 rate global out2; out1 = 1; out2 = round(10.*rand(10,1)) out2(1,1); out2(end,1);
%endfunctionInit
%-------------------------------------------------------------------- % Output %-------------------------------------------------------------------- function Output(block) global out1; % Simply output global values at different rates global out2;
parfor j=1:10 if(block.OutputPort(2).IsSampleHit)
block.OutputPort(2).Data = out2(j); if(block.OutputPort(1).IsSampleHit) if (j==4)
block.OutputPort(1).Data = out2(); end end %out1 = out1 + 1; end
%disp( block.OutputPort(2).Data ) end
% k=0; % while k<1000000000 % k=k+1; %out2 = out2 + 1; % end
%endfunction Output
function Update(block)
%endfunction Update
function Terminate(block) %endfunction Terminate
end
0 Comments
Answers (1)
Hari
on 11 Jun 2025
Hi,
I understand that you are trying to implement a metaheuristic algorithm (like Genetic Algorithm or PSO) inside an S-function, and you want each individual in the population to be evaluated one-by-one per simulation step. However, due to the for loop (or parfor), the loop completes instantly within a single time step, and your S-function always outputs only the last individual’s data.
I assume that you want the S-function to output a new individual at each time step, synchronized with the simulation time, rather than evaluating the entire population in one go.
In order to synchronize the output of individuals with the simulation time, you can follow the below steps:
Step 1: Store the current individual index using DWork or a persistent variable.
This helps track which individual is to be evaluated at each simulation step.
Step 2: In the Output method, use the current index to output the corresponding individual’s data.
This ensures only one individual is processed per step.
Step 3: In the Update method, increment the individual index by 1 (with wrap-around if needed).
This updates the index to be used in the next simulation step.
Step 4: Set the sample time appropriately to control the pacing of individual evaluations.
For example, if you want one individual per 0.3 sec, set the output port sample time accordingly.
Step 5: Avoid using for or parfor loops to iterate over the entire population inside the Output function.
These loops run too fast and are not synchronized with the simulation clock, causing unintended behavior.
References:
Hope this helps!
0 Comments
See Also
Categories
Find more on Block Authoring and Simulation Integration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!