function empcblock_MaskInitFcnResize(blk,nv,nmv)
% EMPCBLOCK_MASKINITFCNRESIZE Modify the I/O structure depending on whether MDs or
% externally supplied MV signals are present
%
% empcblock_MaskInitFcnResize is called in the mask initialization code.  The
% initialization code initializes the masked subsystem at critical times,
% such as model loading and the start of a simulation run.
%
% NV, NU and NY values are provided by empcblock_MaskInitFcnParameter
% or mempcblock_MaskInitFcnParameter.  NV must be at least 1.

%   Authors: A. Bemporad
%   Copyright 1986-2013 The MathWorks, Inc.

%% Obtain current availability of the optional in/out ports in the block
AvailableBlocks = get_param(blk,'Blocks');
type_in_block = ismember({'md','ext.mv','switch'},AvailableBlocks);
type_out_block = ismember({'status','region','est.state','u0'},AvailableBlocks);

%% Obtain availability of the optional in/out ports from the block dialog
type_in_mask = [...
                strcmp(get_param(blk,'md_inport'),'on'), ...
                strcmp(get_param(blk,'mv_inport'),'on'),...
                strcmp(get_param(blk,'switch_inport'),'on'),...
                ];
type_out_mask = [...
                strcmp(get_param(blk,'return_status'),'on'), ...
                strcmp(get_param(blk,'return_region'),'on'), ...
                strcmp(get_param(blk,'return_state'),'on'), ...
                strcmp(get_param(blk,'return_u0'),'on'), ...
                ];

%% Reset mask display to two inports (MO, REF) and one outport (MV)
maskdisplay=get_param(blk,'MaskDisplay');
aux='port_label(''input'', 2, ''ref'')';
pos=strfind(maskdisplay,aux)+length(aux);
maskdisplay=maskdisplay(1:pos-1);
nextInport = 3;
nextOutport = 2;

%% Update MO or X
CustomEstimation = strcmp(get_param(blk,'state_inport'),'on');
if CustomEstimation
    label = '''mo''';
    pos = strfind(maskdisplay, label);
    if ~isempty(pos)
        % Change mo inport to x inport
        maskdisplay = [maskdisplay(1:pos-1),'''x[k|k]''',maskdisplay(pos+length(label):end)];
    end
else
    label = '''x[k|k]''';
    pos = strfind(maskdisplay, label);
    if ~isempty(pos)
        % Change x inport to mo inport
        maskdisplay = [maskdisplay(1:pos-1),'''mo''',maskdisplay(pos+length(label):end)];
    end
end

%% Update MD
[maskdisplay, nextInport] = mpc_update_inport(blk,AvailableBlocks,type_in_mask,type_in_block,1,'md',nextInport,3,maskdisplay);

%% Update EXT.MV
[maskdisplay, nextInport] = mpc_update_inport(blk,AvailableBlocks,type_in_mask,type_in_block,2,'ext.mv',nextInport,4,maskdisplay);

%% Update SWITCH
[maskdisplay, nextInport] = mpc_update_inport(blk,AvailableBlocks,type_in_mask,type_in_block,3,'switch',nextInport,5,maskdisplay); %#ok<ASGLU>

%% Update STATUS
[maskdisplay, nextOutport] = mpc_update_outport(blk,AvailableBlocks,type_out_mask,type_out_block,1,'status',nextOutport,2,maskdisplay);

%% Update REGION
[maskdisplay, nextOutport] = mpc_update_outport(blk,AvailableBlocks,type_out_mask,type_out_block,2,'region',nextOutport,3,maskdisplay);

%% Update Estimated State
[maskdisplay, nextOutport] = mpc_update_outport(blk,AvailableBlocks,type_out_mask,type_out_block,3,'est.state',nextOutport,4,maskdisplay);

%% Update u0
[maskdisplay, nextOutport] = mpc_update_outport(blk,AvailableBlocks,type_out_mask,type_out_block,4,'u0',nextOutport,5,maskdisplay); %#ok<ASGLU>

%% Update Mask Display
set_param(blk,'MaskDisplay',maskdisplay);

%% Now set correct dimensions for zero signals
AvailableBlocks = get_param(blk,'Blocks');
if ismember('md_zero',AvailableBlocks)
    set_param([blk '/md_zero'],'Value',sprintf('zeros(%d,1)',max(nv-1,1)));
end
if ismember('ext.mv_zero',AvailableBlocks)
    set_param([blk '/ext.mv_zero'],'Value',sprintf('zeros(%d,1)',nmv));
end
