%% Change Signal Types of Existing Controller
% To modify the signal types for an existing MPC controller, you must simultaneously modify any controller properties that depend on the signal type configuration.
%
% Create a plant model with two outputs, one manipulated variable, one measured disturbance, and two unmeasured disturbances.

plant = rss(3,2,5);
plant.D = 0;
plant = setmpcsignals(plant,'MV',[1 2],'MD',3,'UD',[4 5]);

% Create an MPC controller using this plant.

MPCobj = mpc(plant,0.1);

% Configure the controller properties. For example, set the scaling factors for the disturbance signals.

MPCobj.DisturbanceVariables(1).ScaleFactor = 10;
MPCobj.DisturbanceVariables(2).ScaleFactor = 5;
MPCobj.DisturbanceVariables(3).ScaleFactor = 20;

%
% Suppose you want to change the second unmeasured disturbance to be a measured disturbance. To do so, you must simultaneously update the DisturbanceVariables property of the controller, since the order of its entries depend on the disturbance types (measured disturbances followed by unmeasured disturbances).
% Create an updated disturbance variable structure array. To do so, move the third element to be the second element.

DV = MPCobj.DisturbanceVariables;
DV = [DV(1) DV(3) DV(2)];
DV(2).Name = 'MD2';

% To set the internal plant model signal types, obtain the Model property from the controller, and modify the signal types of its Plant element.

model = MPCobj.Model;
model.Plant = setmpcsignals(model.Plant,'MV',[1 2],'MD',[3 5],'UD',4);

% Set the model and disturbance variable properties of the controller to their updated values.

set(MPCobj,'Model',model,'DisturbanceVariables',DV);

%
% In general, it is best practice to not modify the signal types after controller creation. Instead, create and configure a new controller object with the new signal configuration.
% Copyright 2019 The MathWorks, Inc.