function newopcond = updateopcond(this)
% UPDATEOPCOND - Create a new operating condition specification object
% given the table data.

%  Author(s): John Glass
%  Revised:
% Copyright 1986-2003 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2004/08/01 00:12:01 $

%% Create a copy of the operating spec object
newopcond = copy(this.OpSpecData);

%% STATES
%% Get the states
states = newopcond.States;

%% Convert to Matlab data types
mStateIndecies = LocalConvertToMatlabInteger(this.StateIndecies);

%% Get the tabledata
Data = this.StateConstrTableModelUDD.data;

%% Set the state values
for ct1 = 1:length(states)
    for ct2 = 1:states(ct1).Nx
        %% Value Column
        try
            variable = Data(mStateIndecies(ct1)+ct2,2);
            data = evalin('base', variable);
            if LocalCheckInvalidData(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                states(ct1).x(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end
        %% Lower Bound Column
        try
            variable = Data(mStateIndecies(ct1)+ct2,5);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                states(ct1).Min(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end        
        %% Upper Bound Column
        try
            variable = Data(mStateIndecies(ct1)+ct2,6);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                states(ct1).Max(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end          
    end
end

%% INPUTS
%% Get the inputs
inputs = newopcond.Inputs;

%% Convert to Matlab data types
mInputIndecies = LocalConvertToMatlabInteger(this.InputIndecies);

%% Get the tabledata
Data = this.InputConstrTableModelUDD.data;

%% Set the input values
for ct1 = 1:length(inputs)
    for ct2 = 1:inputs(ct1).PortWidth
        %% Value Column
        try
            variable = Data(mInputIndecies(ct1)+ct2,2);
            data = evalin('base', variable);
            if LocalCheckInvalidData(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                inputs(ct1).u(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end
        %% Minimum Value Column
        try
            variable = Data(mInputIndecies(ct1)+ct2,4);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                inputs(ct1).Min(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end        
        %% Maximum Value Column
        try
            variable = Data(mInputIndecies(ct1)+ct2,5);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                inputs(ct1).Max(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end          
    end
end

%% OUTPUTS
%% Get the outputs
outputs = newopcond.Outputs;

%% Convert to Matlab data types
mOutputIndecies = LocalConvertToMatlabInteger(this.OutputIndecies);

%% Get the tabledata
Data = this.OutputConstrTableModelUDD.data;

%% Set the input values
for ct1 = 1:length(outputs)
    for ct2 = 1:outputs(ct1).PortWidth
        %% Value Column
        try
            variable = Data(mOutputIndecies(ct1)+ct2,2);
            data = evalin('base', variable);
            if LocalCheckInvalidData(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                outputs(ct1).y(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end
        %% Minimum Value Column
        try
            variable = Data(mOutputIndecies(ct1)+ct2,4);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                outputs(ct1).Min(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end        
        %% Maximum Value Column
        try
            variable = Data(mOutputIndecies(ct1)+ct2,5);
            data = evalin('base', variable);
            if LocalCheckInvalidDataAllowInf(data)
                errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
                lasterr(sprintf('%s. The variable %s must be a real double of length 1',errortitle,variable));
                return
            else
                outputs(ct1).Max(ct2) = data;
            end
        catch
            errortitle = sprintf('Error evaluating the operating point specification: %s',this.Label); 
            str = sprintf('%s. Invalid workspace variable - %s',errortitle, variable);
            lasterr(str);
            return
        end          
    end
end

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function to check for valid data
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function isinvalid = LocalCheckInvalidData(data)

isinvalid = ~(isa(data,'double') && ...
              length(data) == 1 && ...
              isreal(data) && ...
              ~isnan(data) && ...
              ~isinf(data));

%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function to check for valid data
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function isinvalid = LocalCheckInvalidDataAllowInf(data)

isinvalid = ~(isa(data,'double') && ...
              length(data) == 1 && ...
              isreal(data) && ...
              ~isnan(data));          
              
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function to convert the java integer array to a Matlab array
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function array = LocalConvertToMatlabInteger(javaarray)

array = zeros(length(javaarray),1);

for ct = 1:length(javaarray)
    array(ct) = intValue(javaarray(ct,1)) + 1;
end