function empcblock_StartFcn
% Check signal connectivity to MPC Controller block
% Signal dimensions are already checked in the C S-function blocks

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

%% Run as open loop when no explicit mpc is present
blk = gcb;
if isempty(get_param(blk,'empcobj'))
    return
end

%% Retrieves MPC dimensions
MPCstruct = get_param(blk,'Userdata');
nmy = MPCstruct.nmy;
nmd = MPCstruct.nv-1;
ny = MPCstruct.ny;
nmv = MPCstruct.nmv;
nx = MPCstruct.nx;

%% Check if inports are connected with corrent dimensions
ports=get_param(blk,'PortConnectivity');
widths=get_param(blk,'CompiledPortWidths');
err='';
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'),'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'),'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