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

%   Authors: R. Chen, A. Bemporad
%   Copyright 1986-2015 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,'Nempc'));
open_loop=0;
for i=1:N
    blkEMPC=sprintf('%s/MPC%d',childblkstr,i);
    empcobjname=get_param(blkEMPC,'empcobj');
    if isempty(empcobjname)
        open_loop=1;
    end
end
if open_loop
    return
end

%% Retrieves MPC dimensions from first Explicit MPC block
blkEMPC1=sprintf('%s/MPC%d',childblkstr,1);
empc1=get_param(blkEMPC1,'empcobj');
EMPCData = get_param(blkEMPC1,'Userdata');
nmy = EMPCData.nmy;
nmd = EMPCData.nmd;
ny = EMPCData.ny;
nmv = EMPCData.nmv;
nx = EMPCData.nx;

%% Check that all Explicit MPC objects share the same dimensions for REF, MO, MV and MD
for i=2:N
    blkEMPC=sprintf('%s/MPC%d',childblkstr,i);
    empcI=get_param(blkEMPC,'empcobj');
    empcIobj = slResolve(empcI,blkEMPC);
    empcIdata = getExplicitMPCData(empcIobj);
    wrong='';
    if ~(empcIdata.ny==ny)
        wrong='output vectors and references';
    end
    if ~(empcIdata.nmv==nmv)
        wrong='manipulated variables';
    end
    if ~(length(empcIdata.myindex)==nmy)
        wrong='measured outputs';
    end
    if ~(length(empcIdata.mdindex)==nmd)
        wrong='measured disturbances';
    end
    if ~isempty(wrong)
        ctrlMsgUtils.error('MPC:utility:InvalidDimension',wrong,empc1,empcI);
    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 EMPCData.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; %#ok<NASGU>
end

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