Export single values measured in Simulink to an array in the MATLAB workspace

7 views (last 30 days)
Hi,
I want to export discrete time measurements (not the whole simulation) to the Matlab Workspace. For this I tried to create an array, which contains all the measurements, by using a Matlabfunction-block. Because my simulation runs with a 0.001s step, Simulink would reinitiate the array every 0.001s. Therefore I tried to intitiate the array in the MATLAB workspace and then determine the number of elemts inside the array by using 'numel'. In the following i wanted to write the next value to the array and save it with a 'To Workspace' block.
Problem is, that Simulink can´t read empty arrays from the workspace. Additionally I would need the array to be variable, because the number of elements can differ from run to run.
The Main Question would be, how I can initialize an array and add values to it, without the array being overwritten or reinitialized everytime.
This was the code I tried:
function y = fcn(array, value_assigned)
array(numel(array))=value_assigned;
y = array;
  1 Comment
Benjamin Thompson
Benjamin Thompson on 2 Mar 2022
Can you just use a To Workspace block with a lower update rate? If you know you wanted to save the data at 5 and 10 seconds, for example, just set the step size or decimation parameter for the block to 5 seconds.
You can also embed a To Workspace block inside an Enabled Subsystem and it will only save while the subsystem is enabled.

Sign in to comment.

Answers (1)

Moksh
Moksh on 14 Nov 2023
Hi Hannes,
I understand that you have generated some time measurements in Simulink and now wish to export these measurements at some specified time intervals.
You can do the following for solving this issue:
  • Setting the “Sample Time” parameter in the “To Workspace” block in Simulink. This specifies when the block logs the input value to this block during simulation. You can specify a discrete sample time parameter as a scalar. Please use the following documentation for more information about the “To Workspace” block and its parameters: https://www.mathworks.com/help/simulink/slref/toworkspace.html
  • You can also export the entire measurements array and extract required readings by iterating over the array post simulation.
Hope this information helps resolve the issue.
Best Regards,
Moksh Aggarwal

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!