function [mpcobj, xmpc0] = mpc_chkmpcobjxmpc(blk)
% Utility function to obtain @mpc and @mpcstate object from block dialog.

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

%% MPC object
% The object name is obtained from the mask as a string.  At this stage, it
% cannot be an empty string.
mpcobjname = strtrim(get_param(blk,'mpcobj'));
% Evaluate it to obtain MPC object.
try
    % Load from workspace
    mpcobj = slResolve(mpcobjname,blk);
catch ME
    throw(ME);
end
% Error out if it is not a @mpc object or the MPC object is empty
if ~isa(mpcobj,'mpc')
    ctrlMsgUtils.error('MPC:designtool:InvalidMPCObject',blk);
end
if isempty(mpcobj)
    ctrlMsgUtils.error('MPC:designtool:EmptyMPCObject',blk);
end
%% Initial states
% The state object name is obtained from the mask as a string.
InitialState = strtrim(get_param(blk,'x0'));
% Evaluate it to obtain MPCSTATE object.
if isempty(InitialState)
    InitialState = '[]';
end
try
    xmpc0 = slResolve(InitialState, blk);
catch ME
    throw(ME);
end
% Error out if it is not a @mpcstate object or it is not []
if ~isempty(xmpc0) && ~isa(xmpc0,'mpcstate')
    ctrlMsgUtils.error('MPC:designtool:InvalidMPCStateObject',blk);
end
