Change the buffer size during a simulation

I am trying to log a reading from a sensor for 2 seconds when simulink starts in order to define a suitable size for the buffers/matrices that I am using in signal analysis.
-Would there be a way to change the size of the buffer after the start of the simulation ?
*I tried to pause the simulation and update the parameters and resume but unfortunately it didn't work, it still gives me the same error: ""Cannot change parameter 'Output buffer size (per channel): (N)' of 'RotorBalancing/Subsystem1/Buffer' while simulation is running""
I used this code for this sake:
set_param('RotorBalancing','SimulationCommand','start');
set_param('RotorBalancing','SimulationCommand','pause');
set_param('RotorBalancing/Subsystem1/Buffer','N','1000')
set_param('RotorBalancing/Subsystem1/Buffer2','N','1000')
set_param('RotorBalancing','SimulationCommand','update');
set_param('RotorBalancing','SimulationCommand','continue');
  • I tried to enable the buffer after the 2 seconds and I still got the same error.
-In case changing the buffer size won't work. Is there a better option to store the signal and prepare it for signal analysis (FFT) ?

 Accepted Answer

Buffer block dialog parameters can not be modified while the simulation is running or paused. You can observe this by trying to interact with the parameters in Simulink environment, and you will notice that they are edit-disabled.
If you want to post-process the signal, you can always log the signal to a MAT file or the workspace and then, use MATLAB functions for FFT to perform the required analysis. The following documentation lists all the relevant functions in MATLAB.
Hope this helps!

7 Comments

Thank you for the answer Saurabh,
I would like to store the signal and postprocess it at the same simulation run. As far as I know (and tried) MAT files and Workspace variables are read/wrote at the the beginning/end of each simulation. Is there a workaround ? I appreciate your help
By post-processing, I also meant to suggest doing the analysis at the end of simulation.
I am not entirely clear about your use case. But if you are trying to determine the appropriate buffer size by analyzing the results, then I would suggest you to run short simulations, do post-processing to determine the next best buffer size, then set that buffer size and re-run the simulation.
You can repeat this process in a script similar to what you mentioned in your original question. The main change I'm proposing is to let the simulation finish (instead of pausing it) before trying to change the value of buffer size.
If this doesn't work, I would suggest posting some details about your overall objective and explain why post-processing the output doesn't suit your requirement.
If I recall correctly, variables are also written when the simulation is paused.
The main intention is to build one simulink block diagram and convert it to C code to run it on a model with some controller.
I want to read the signal from the sensors of the model, log it, perfrom FFT, and then apply a certain algorithm. This algorithm will change some parameters in the model in the following steps which will in turn change the signal output of the sensors, and those will be stored, analyzed by FFT, and stored in other variables and fed again to the countinously running algorithm.
It is not necessary to use a buffer if there is another method to store the signal.
@Walter I am trying to use this suggestion but didn't succeed so far, thanks !
You might possibly need to switch over to doing fair parts of it as a MATLAB Function block that uses coder.varsize to allow variable sized arrays.
The buffer size changes that might be desired: are there a fairly limited number of different permitted sizes, such that you might be able to use a switch block or triggered subsystems or the like, one copy of the subsystem for each of the limited number of different sizes?
Thank you again for the help
Actually it would much more convenient to me to store the signal in other than buffers cuz I am facing another limitation as described here: https://de.mathworks.com/matlabcentral/answers/352923-how-is-the-buffer-block-filled-in-an-enabled-subsystem
Can the signal be stored in a Matlab function block or you only suggest it for signal-processing ?
Sorry, I do not know much about those topics.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!