function [Uscale, Yscale] = mpc_scalefactors(MPCData, MV, DV, OV)
% MPC_SCALEFACTORS Get scale factors from MPC object
% Private utility function retrieves vectors of scale factors from mpc
% object "Obj".  

% Author:  L. Ricker
% Copyright 1994-2013 The MathWorks, Inc.

% Uscale:   Vector of input scale factors, one for each plant input.
% Yscale:   Vector of output scale factors, one for each plant output.

%% Handle empty MPCData 
% Can happen when loading an old object using LOADOBJ method
if isempty(MPCData)
    Uscale = [];
    Yscale = [];
    return
end
%%
% Initialization to default values
Uscale = ones(MPCData.nu,1);
Yscale = ones(MPCData.ny,1);
nmv = MPCData.nmv;
nmd = MPCData.nmd;
nud = MPCData.nud;
ny  = MPCData.ny;
% Verify that scale factor properties have been defined.  If not, assume 
% mpc object was created in earlier toolbox version and use defaults.
if nargin == 1
    % MPCData supplied.  For use when MPCData should be initialized.  
    % Example:  call from mpc/sim.
    Uscale = MPCData.Uscale;
    Yscale = MPCData.Yscale;
elseif nargin == 4
    % Used by mpc_construct to extract scale factors from other mpc
    % object fields.  mpc_construct will store them in MPDData.
    for i = 1:nmv
        Uscale(MPCData.mvindex(i),1) = MV(i).ScaleFactor;
    end
    for i = 1:nmd
        Uscale(MPCData.mdindex(i),1) = DV(i).ScaleFactor;
    end
    for i = 1:nud
        Uscale(MPCData.udindex(i),1) = DV(nmd+i).ScaleFactor;
    end
    for i = 1:ny
        Yscale(i) = OV(i).ScaleFactor;
    end
end