From 3481f1fbdb3926907d6a92873b13b073cd350bdf Mon Sep 17 00:00:00 2001
From: Federico Felici <federico.felici@epfl.ch>
Date: Mon, 13 Jan 2020 20:06:14 +0100
Subject: [PATCH] Cleanup and add support for recursive buses

---
 code/functions/SCDconf_createsimdatastruct.m | 37 +++++++++++---------
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/code/functions/SCDconf_createsimdatastruct.m b/code/functions/SCDconf_createsimdatastruct.m
index 4a0ef02..591c3fc 100644
--- a/code/functions/SCDconf_createsimdatastruct.m
+++ b/code/functions/SCDconf_createsimdatastruct.m
@@ -5,27 +5,32 @@
 % 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_createsimdatastruct(bus)
+function [out] = SCDconf_createsimdatastruct(dd,busname)
 
+bus=dd.getEntry(busname).getValue; % get from data dictionary
 bussize=size(bus.Elements);
 signalssize=bussize(1);
 
-tempstruct=struct();
 for ii=1:signalssize
-    elementdim=bus.Elements(ii).Dimensions;
-    switch numel(elementdim)
-        case 1,
-            data=zeros(elementdim(1),2)';
-        case 2,
-            data=zeros(elementdim(1),elementdim(2),2);
-        otherwise,
-            error('Not supported data structure');
-    end 
-    tscmd=sprintf('tmpts=timeseries(%s(data),[0;1]);',bus.Elements(ii).DataType);
-    eval(tscmd);
-    structcmd=sprintf('tempstruct.%s=tmpts;',bus.Elements(ii).Name);
-    eval(structcmd);
+  element=bus.Elements(ii);
+  if contains(element.DataType,'Bus: ')
+    busname = erase(element.DataType,'Bus: ');
+    % recursive call
+    out.(element.Name) = SCDconf_createsimdatastruct(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=tempstruct;
 %out=Simulink.SimulationData.createStructOfTimeseries(bus,tempstruct)
\ No newline at end of file
-- 
GitLab