How can I programmatically get the dimensions of a Simulink signal?

7 views (last 30 days)
I would like to save a large Simulink bus to a file as an array instead of a time series because the model then runs about 2 times faster. In order to match the rows in the array with the signals in the bus I need to be able to get the signal names and dimensions. The signals in the bus are changing at this stage of the model development, so a fixed list would be very cumbersome and error-prone. I can programmatically get the names of the signals, but so far I haven't been able to find any way to get the dimensions.
Any ideas? Thanks!

Accepted Answer

Stefan Raab
Stefan Raab on 20 Apr 2016
Hello,
you could use Simulink.Bus.createObject and create a Bus object of the bus that is sent to workspace, for example like:
Simulink.Bus.createObject(gcs,[gcs '/Bus Creator1'],'somefilename');
In this, gcs is your current system and the path to your Bus Creator has to be adapted. This will create Bus objects in the Base Workspace for the chosen bus and also the bus signals included. I wrote a function, that loops through this and just displays the included signals and their dimensions. It also checks the base workspace for the existence of the temporary BusElement. If yes, it might be a bus itself, if not, it is most likely a signal. Before you run the script, you will also need the Base Workspace variables saved in a cell, which could be achieved by this:
BWS_vars = who;
for i=1:length(BWS_vars)
BWS_vars{i,2} = eval(BWS_vars{i,1});
end
But note: If there are busses and signals within your model that have the same name, the script would cause an infinite loop. In order to avoid that, I included a timer and the script will stop after 2 seconds. Here is the function, call it with the BusObject that contains the signal you want to log as array and with the base workspace cell:
function getSignalNamesAndDimensions(BusObject,BWS_vars,timer)
% Loops through the BusObject from the first element to the last and displays name and sizes of the signals
if nargin<3
timer = tic;
end
if toc(timer) > 2
error('Possible infinite loop!');
end
n_Elements = length(BusObject.Elements);
for ind = 1:n_Elements
element_temp = BusObject.Elements(ind);
% Check if Object is in Workspace -> Might be a bus
if any(strcmp(BWS_vars(:,1),element_temp.Name))
% Check if WS Variable is a Simulink.Bus object
busCheck = BWS_vars(strcmp(BWS_vars(:,1),element_temp.Name)==1,2);
if isa(busCheck{1},'Simulink.Bus')
% Recursive call of this function
getSignalNamesAndDimensions(busCheck{1},BWS_vars,timer);
end
else
% BusElement is signal
fprintf([element_temp.Name '\t' num2str(element_temp.Dimensions) '\n']);
% Save in struct/cell
% ...
end
end
end
I hope this might help you.
Kind regards, Stefan

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!