function mmpcblock_StartFcn
% Check signal connectivity to MPC Controller block
% Signal dimensions are already checked in the each MPC block

%   Authors: R. Chen
%   Copyright 1986-2014 The MathWorks, Inc.

blk = gcb;
childblkstr=[get_param(blk,'Parent') '/' get_param(blk,'Name')];

%% Run as open loop when no mpcobj in some of the MPC blocks
N=str2double(get_param(blk,'Nmpc'));
open_loop=0;
for i=1:N,
    blkMPC=sprintf('%s/MPC%d',childblkstr,i);
    mpcobjname=get_param(blkMPC,'mpcobj');
    if isempty(mpcobjname)
        open_loop=1;
    end
end
if open_loop
    return
end

%% Retrieves MPC dimensions from first MPC block
blkMPC1=sprintf('%s/MPC%d',childblkstr,1);
mpc1=get_param(blkMPC1,'mpcobj');
MPCstruct = get_param(blkMPC1,'Userdata');
nmy = MPCstruct.nmy;
nmd = MPCstruct.nv-1;
ny = MPCstruct.ny;
nmv = MPCstruct.nmv;
nx = MPCstruct.nx;

%% Check that all MPC objects share the same dimensions for REF, MO, MV and MD
for i=2:N,
    blkMPC=sprintf('%s/MPC%d',childblkstr,i);
    mpcI=get_param(blkMPC,'mpcobj');
    mpcIobj = slResolve(mpcI,blkMPC);
    wrong='';
    if ~(getNY(mpcIobj)==ny)
        wrong='output vectors and references';
    end
    if ~(getNMV(mpcIobj)==nmv)
        wrong='manipulated variables';
    end
    if ~(getNMY(mpcIobj)==nmy)
        wrong='measured outputs';
    end
    if ~(getNMD(mpcIobj)==nmd)
        wrong='measured disturbances';
    end
    if ~isempty(wrong)
        ctrlMsgUtils.error('MPC:utility:InvalidDimension',wrong,mpc1,mpcI);
    end
end

%% Check all the mandatory inports (SWITCH, MO and REF) are connected with corrent dimensions
ports=get_param(blk,'PortConnectivity');
widths=get_param(blk,'CompiledPortWidths');
err='';
portnum=1;

%% Check SWITCHING SIGNAL inport is connected with corrent dimensions
if ports(portnum).SrcBlock < 0
    err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','switch',1));
end
portnum=portnum+1;

%% Check whether mandatory MEASURED OUTPUT (or STATE) inport is connected
if MPCstruct.CustomEstimation
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','x[k|k]',nx));
    end
else
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','mo',nmy));
    end
end
portnum=portnum+1;

%% Check REFERENCE inport is connected
if ports(portnum).SrcBlock < 0
    err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','ref',ny));
end
portnum=portnum+1;

%% Check MD inport is connected with corrent dimension when present
MDenabled = strcmp(get_param(blk,'md_inport_multiple'),'on');
if length(widths.Inport)>=portnum && MDenabled
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','md',nmd));
    end
    portnum=portnum+1;
end

%% Check EXT.MV inport is connected with corrent dimension when present
MVenabled = strcmp(get_param(blk,'mv_inport_multiple'),'on');
if length(widths.Inport)>=portnum && MVenabled
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','ext.mv',nmv));
    end
    portnum=portnum+1;
end

%% Check UMIN inport is connected with corrent dimension when present
UMINenabled = strcmp(get_param(blk,'umin_inport_multiple'),'on');
if length(widths.Inport)>=portnum && UMINenabled
    if ports(portnum).SrcBlock < 0
        fprintf('%s\n',getString(message('MPC:utility:LimitUnconnected','umin')));
    end
    portnum=portnum+1;
end

%% Check UMAX inport is connected with corrent dimension when present
UMAXenabled = strcmp(get_param(blk,'umax_inport_multiple'),'on');
if length(widths.Inport)>=portnum && UMAXenabled
    if ports(portnum).SrcBlock < 0 
        fprintf('%s\n',getString(message('MPC:utility:LimitUnconnected','umax')));
    end
    portnum=portnum+1;
end

%% Check YMIN inport is connected with corrent dimension when present
YMINenabled = strcmp(get_param(blk,'ymin_inport_multiple'),'on');
if length(widths.Inport)>=portnum && YMINenabled
    if ports(portnum).SrcBlock < 0 
        fprintf('%s\n',getString(message('MPC:utility:LimitUnconnected','ymin')));
    end
    portnum=portnum+1;
end

%% Check YMAX inport is connected with corrent dimension when present
YMAXenabled = strcmp(get_param(blk,'ymax_inport_multiple'),'on');
if length(widths.Inport)>=portnum && YMAXenabled
    if ports(portnum).SrcBlock < 0
        fprintf('%s\n',getString(message('MPC:utility:LimitUnconnected','ymax')));
    end
    portnum=portnum+1;
end

%% Check YWT inport is connected with corrent dimension when present
YWTenabled = strcmp(get_param(blk,'ywt_inport_multiple'),'on');
if length(widths.Inport)>=portnum && YWTenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','y.wt',ny));
    end
    portnum=portnum+1;
end

%% Check YWT inport is connected with corrent dimension when present
UWTenabled = strcmp(get_param(blk,'uwt_inport_multiple'),'on');
if length(widths.Inport)>=portnum && UWTenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','u.wt',nmv));
    end
    portnum=portnum+1;
end

%% Check DUWT inport is connected with corrent dimension when present
DUWTenabled = strcmp(get_param(blk,'duwt_inport_multiple'),'on');
if length(widths.Inport)>=portnum && DUWTenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','nu.wt',nmv));
    end
    portnum=portnum+1;
end

%% Check RHOEPS inport is connected with corrent dimension when present
RHOEPSenabled = strcmp(get_param(blk,'rhoeps_inport_multiple'),'on');
if length(widths.Inport)>=portnum && RHOEPSenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','ecr.wt',1));
    end
    portnum=portnum+1; 
end

%% Check UREF inport is connected with corrent dimension when present
UREFenabled = strcmp(get_param(blk,'uref_inport_multiple'),'on');
if length(widths.Inport)>=portnum && UREFenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','mv.target',nmv));
    end
    portnum=portnum+1; 
end

%% Error out
if ~isempty(err)
     ctrlMsgUtils.error('MPC:utility:InvalidBlockSignalDimensionGeneral',err);
end
