Skip to content
Snippets Groups Projects
SCDconf_createstructfrombus.m 1.24 KiB
Newer Older
% This function helps creating template simulation input structures
% for From Workspaces blocks feeding bus signals
% Called with a Simulink.Bus object as an input it creates
% a template struct of timeseries filled with 2 timepoints [0,1]
% and with zero data of the correct kind
% This is enough for passing a ctrl-D with from Workspace bus kind blocks

function [out] = SCDconf_createstructfrombus(dd,busname)
bus=dd.getEntry(busname).getValue; % get from data dictionary
bussize=size(bus.Elements);
signalssize=bussize(1);

for ii=1:signalssize
  element=bus.Elements(ii);
  if contains(element.DataType,'Bus: ')
    busname = erase(element.DataType,'Bus: ');
    % recursive call
    out.(element.Name) = SCDconf_createstructfrombus(dd,busname);
  else
    try cast(1,element.DataType); catch ME; error('non-numeric type'); end
    elementdims=element.Dimensions;
    switch numel(elementdims)
      case 1
        data=zeros(elementdims(1),2)';
      case 2
        data=zeros(elementdims(1),elementdims(2),2);
      otherwise
        error('Not supported data structure');
    end
    out.(element.Name) = ...
      timeseries(cast(data,element.DataType),[0;1],'Name',element.Name);
  end
end

%out=Simulink.SimulationData.createStructOfTimeseries(bus,tempstruct)