function [blk,Ts,A,Cm,Bu,Bv,Dvm,M,L,strmoorx,nmoorx,no_mv,nmv,nv,ny,nx,...
    lastx,lastu,mvoff,yoff,mdoff,myoff,no_md,no_ref,...
    switch_inport,no_switch,enable_value,...
    openloopflag,return_status,return_region,...
    F,G,H,K,i1,i2,nr,np,xunfindex,refunfindex,uunfindex,mdunfindex,...
    thmin,thmax,CustomEstimation,xoff,...
    RYscale,RMYscale,RMVscale,RMDscale,MVscale,MDscale] = empcblock_MaskInitFcnParameter
%EMPCBLOCK_MASKINITFCNPARAMETER Get all necessary matrices needed by the EMPC S-function

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

% EMPC block parameter "UserData" can have the following valid data sets
% depending on the use cases:
%   []          - when SL model is open/loaded or design from scratch
%   EMPData   - otherwise
blk = gcb;
EMPCData=get_param(blk,'Userdata');
DataType = get_param(blk,'BlockDataType');
ONE = ones(DataType);
ZERO = zeros(DataType);
root=bdroot(blk);
EMPCobjname=get_param(blk,'empcobj');

% When open SL model, EMPCData is empty
if isempty(EMPCData)
    openloopflag = true;
    %% Non-required parameters for this operation
    [yoff,mdoff,no_md,no_mv,no_ref,switch_inport,no_switch,enable_value,...
        return_status,return_region,F,G,H,K,i1,i2,nr,np,xunfindex,refunfindex,uunfindex,mdunfindex,thmin,thmax] = deal(ZERO);
    nmv = ONE;
    nmy = ONE;
    nv = ONE;
    nx = nmy; % set # states equal #MO
    ny = nmy; % because #UO is 0 in design from scratch, #MO = #REF
    %% Sample time (must be in double precision)
    Ts = 1;
    %% Required parameters
    lastu = zeros(nmv,1,DataType);   % unit delay - Last MVs
    lastx = zeros(nx,1,DataType);   % memory block
    mvoff = zeros(nmv,1,DataType);    % bias - mv_bias
    myoff = zeros(nmy,1,DataType);  % state estimator 
    Cm = zeros(nmy,nx,DataType);    % state estimator 
    Dvm = zeros(nmy,nv,DataType);   % state estimator 
    M = zeros(nx,nmy,DataType);     % state estimator 
    L = zeros(nx,nmy,DataType);     % state estimator 
    A = zeros(nx,nx,DataType);      % state estimator 
    Bu = zeros(nx,nmv,DataType);     % state estimator 
    Bv = zeros(nx,nv,DataType);     % state estimator 
    CustomEstimation = false;         % state estimator 
    xoff = zeros(nx,1,DataType);    % state estimator 
    MVscale = ones(nmv,1,DataType); % scale factors for manipulated variables
    MDscale = ONE;                  % scale factors for measured disturbances
    
    RMYscale = ones(nmy,1,DataType); % scale factors for measured outputs
    RMVscale = ones(nmv,1,DataType);  % scale factors for manipulated variables
    RMDscale = ONE;                  % scale factors for measured disturbances
    RYscale = ones(ny,1,DataType);   % Scale factors for all outputs and references
    
else
    openloopflag = false;
    %% Check start time
    StartTimeExpression = get_param(root,'StartTime');
    try
        t0 = slResolve(StartTimeExpression, root);
    catch %#ok<CTCH>
        ctrlMsgUtils.error('MPC:utility:InvalidStartTime');
    end
    if ~isreal(t0) || ~isscalar(t0) || t0<0
        ctrlMsgUtils.error('MPC:utility:InvalidStartTime');
    end
    %% get EMPC parameters 
    % convert them into correct data type except for sample time "Ts" which
    % must be in double format
    names=fieldnames(EMPCData);
    for i=1:length(names)
        aux=names{i};
        eval(sprintf('%s=EMPCData.%s;',aux,aux));
        if ~strcmp(aux,'Ts') && strcmp(DataType,'single') && isnumeric(eval(aux))
            eval(sprintf('%s=single(%s);',aux,aux));
        end
    end
    %% EML block doesn't like empty parameters
    % even if they are not used in computation at all. Transform any of
    % them to zero matrices with correct size.
    %#ok<*NODEF>
    if isempty(mdoff) % [inf 1]
        mdoff=ZERO;
    end
    if isempty(xunfindex) % [inf 1]
        xunfindex=ZERO;
    end
    if isempty(refunfindex) % [inf 1]
        refunfindex=ZERO;
    end
    if isempty(uunfindex) % [inf 1]
        uunfindex=ZERO;
    end
    if isempty(mdunfindex) % [inf 1]
        mdunfindex=ZERO;
    end
    
    %% save structure to user data
    set_param(blk,'Userdata',EMPCData);
    
    %% Obtain EMPC object and error out if it is invalid
    try
        % Load from workspace
        EMPCobj = slResolve(EMPCobjname,blk);
    catch ME
        throw(ME);
    end
    if ~isa(EMPCobj,'explicitMPC')
        ctrlMsgUtils.error('MPC:object:EMPCInvalidObject',blk);
    end
    
    % Transform partition to struct array to matrices
    [F,G,H,K,i1,i2]=getPiecewiseAffineMatricies(EMPCobj); 
    
    if strcmp(DataType,'single')
        F=single(F);
        G=single(G);
        H=single(H);
        K=single(K);
        i1=single(i1);
        i2=single(i2);
    end
    
end

if strcmp(get_param(blk,'SampleTimeInherited'),'on')
    Ts = -1;
end

if CustomEstimation
    nmoorx = nx; %#ok<UNRCH>
    strmoorx = 'x[k|k]';
else
    nmoorx = nmy;
    strmoorx = 'mo';
end
