function impcblock_StartFcn
% MPC block uses three functions in the following order to initialize:
%
%   InitFcn: collect data from @mpc and @mpcstate objects specified in the
%   block dialog.  Obtain additional inport and outport connection
%   information (i.e. optional ports on/off status, whether an inport is
%   connected to a source block), special mmpc settings, and some scaling
%   data.  Save the structure to block "UserData".
%
%   MaskInitFcn: split into two functions: Parameter and Resize.
%
%       Parameter: when "UserData" is empty (i.e. model opens or no @mpc),
%       initialize the block with correct information for compilation.
%       Otherwise, create mask variables from "UserData" with additional
%       sanity check and memory optimization.
%
%       Resize: modify the block I/O based on dialog settings.  Each
%       constant block (when an optional inport is off) is initialized with
%       correct dimension.
%
%   StartFcn: check signal connectivity to MPC Controller block.  Signal
%   dimensions are already checked in the C S-function blocks connected to
%   the inports.  This function is called after compilation is done.

%   Authors: A. Bemporad, N. L. Ricker, R. Chen
%   Copyright 1986-2017 The MathWorks, Inc.

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

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

%% If block is inside a multiple mpc block, the disconnection check is unnecessary
if MPCstruct.is_multiple
    return
end

%% 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',nym));
    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',nu));
    end
    portnum=portnum+1;
end

%% Check UMIN inport is connected with corrent dimension when present
UMINenabled = strcmp(get_param(blk,'umin_inport'),'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'),'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'),'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'),'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 QP switch inport are connected with corrent dimension when present
QPSwitchOn = strcmp(get_param(blk,'switch_inport'),'on');
if length(widths.Inport)>=portnum && QPSwitchOn
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','switch',1));
    end
    portnum=portnum+1;
end

%% Check YWT inport is connected with corrent dimension when present
YWTenabled = strcmp(get_param(blk,'ywt_inport'),'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'),'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',nu));
    end
    portnum=portnum+1;
end

%% Check DUWT inport is connected with corrent dimension when present
DUWTenabled = strcmp(get_param(blk,'duwt_inport'),'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',nu));
    end
    portnum=portnum+1;
end

%% Check RHOEPS inport is connected with corrent dimension when present
RHOEPSenabled = strcmp(get_param(blk,'rhoeps_inport'),'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'),'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',nu));
    end
    portnum=portnum+1; 
end

%% Check EXT.P and EXT.M inports are connected with corrent dimension when present
PMenabled = strcmp(get_param(blk,'pm_inport'),'on');
if length(widths.Inport)>=portnum && PMenabled 
    if ports(portnum).SrcBlock < 0
        err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','ext.p',1));
    end
    portnum=portnum+1; 
end
if length(widths.Inport)>=portnum && PMenabled 
    if ports(portnum).SrcBlock < 0
        %err = sprintf('%s%s\n',err,ctrlMsgUtils.message('MPC:utility:InvalidBlockSignalDimensionNoSignal','ext.m',1));
        
        % any vector of blocking moves is fine in principle
    end
    portnum=portnum+1; 
end

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