How can I store a bus signal to the MATLAB workspace while retaining its hierarchy in Simulink 7.6 (R2010b)?

78 views (last 30 days)
In a complex Simulink model, I want to export data from many different signals. I would like to be able to create a bus with all of the relevant signals in my model and store the bus signal as a structure in the MATLAB workspace with the signal names and hierarchy preserved so that it is easy to analyze.
I have tried using the To Workspace block and an Outport block but these flatten the signal into an array when stored in the base workspace.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Feb 2021
Edited: MathWorks Support Team on 17 Feb 2021
There are several ways to store bus signals in the MATLAB workspace. Please choose one of the following three approaches:
1. Starting with Simulink 7.9 (R2012a), the 'To Workspace' block can be used to store bus signals of any mixed data when using the MATLAB 'Timeseries' format. For more information on this, please refer to the following link:
2. To store bus signals in Simulink 7.6 (R2010b) in the MATLAB workspace as a structure with the same hierarchy and signal names, data logging can be used as follows:
1) Right click on the desired bus signal and select Signal Properties.
2) In the Signal Properties window, check the box for 'Log signal data'.
3) In the model's Configuration Parameters dialog box, select Data Import/Export in the left pane.
4) Select the Signal logging checkbox and specify the variable name where the bus signal will be stored.
The logged signal variable will be of the form:
variablename.busname.signalname
3. Unfortunately, the above methods do not work for models compiled with Real-Time Workshop. This is because Real-Time Workshop does not support signal logging. An alternative method can be used as a workaround, however it is less direct. The following steps demonstrate how to create a MATLAB structure from a bus signal's flattened array and the accompanying model.
1) Save the bus signal to a MAT-file using the To File block and select 'MAT-file logging' under Configuration Parameters>Real-Time Workshop>Interface>Data exchange
2) Compile and run the model and executable, respectively, to generate the MAT-file with the bus signal stored as an array.
3) From the Simulink model, use the following code to create a Simulink bus object:
busInfo = Simulink.Bus.createObject(mdlName, blkName);
num_el = eval([busInfo.busName '.getNumLeafBusElements']);
elemList = eval([busInfo.busName '.getLeafBusElements']);
4) Create an array of Timeseries objects that capture the signal data from your MAT-file:
load MyFile % generated in the second step
for i = 1:num_el
size = elemList(i).Dimensions;
ts{i} = timeseries(data(i+1:i+size,:)',data(1,:)');
end
5) Finally, propagate the Simulink bus object with the above timeseries using the CREATESTRUCTOFTIMESERIES method:
MYBUS = Simulink.SimulationData.createStructOfTimeseries(busInfo.busName,ts);

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!