% load model
timeStart = -1;
timeEnd = -1;
h = new_system;
sName = get_param(h, "Name");
y = 0;
% parse vars
lst_times = split(TIME_LIST, ",");
for i = 1 : size(lst_times)
    sgn_time = lst_times{i};
    lst_sgns_val = eval(sgn_time + "_LIST");    
    lst_sgns = split(lst_sgns_val, ",");
    for j = 1 : size(lst_sgns)
        sgn = lst_sgns{j};
        if exist(sgn, "var") == 1
            % get time start
            if -1 == timeStart
                s = sprintf("timeStart = %s(1);", sgn_time);
            else
                s = sprintf("timeStart = min(timeStart, %s(1));", sgn_time);
            end
            eval(s);
            % get time end
            if -1 == timeEnd
                s = sprintf("timeEnd = %s(length(%s));", sgn_time, sgn_time);
            else
                s = sprintf("timeEnd = max(timeEnd, %s(length(%s)));", sgn_time, sgn_time);
            end
            eval(s);
            % make timeseries
            s = sprintf("%s = timeseries(%s, %s);", sgn, sgn, sgn_time);
            eval(s);
            % add in ui
            sVar = sName + "/" + sgn;
            add_block("simulink/Sources/From Workspace", sVar);
            set_param(sVar, 'position', [100, y, 400, y + 15]);
            set_param(sVar, 'VariableName', sgn);
            y = y + 30;
        end        
    end
end
% set model start and stop time
if timeStart == -1
    timeStart = 0
end
if timeEnd == -1
    timeEnd = 10;
end
set_param(h, 'StartTime', num2str(timeStart));
set_param(h, 'StopTime', num2str(timeEnd));
set_param(h, 'SolverType', 'Fixed-step');
set_param(h, 'Solver', 'FixedStepDiscrete');
set_param(h, 'FixedStep', '0.005');
% open model
open_system(h);

