function TMS = TMSDemoMC(Variable_Mat,Variable_Names,MPNum,Conditions,TMSDesign)
%TMS = TMSDemoMC(Variable_Mat,Variable_Names,MPNum,Conditions,Design)
%THIS ROUTINE RUN A MONTE CARLO ANALYSIS FOR A GIVEN MISSIONPOINT
%
%INPUTS
%  Variable_Mat   = Matrix of variable values,  columns are variables, rows
%                   are values
%  Variable_Names = vector of names corresponding to Variable_Mat columns
%                   These names must match the variable names in the
%                   Conditions structure
%  MPNum          = Mission Point Number, will correspond to Conditions
%                   array
% Conditions      = array from reading the mission file
% Design          = Design structure from the HEATS sizing run
%
%OUTPUT
%  TMS  Output cell array, TMS{i} corresponds to Variable_Mat(i,:);

TMSDesign.Conditions = Conditions{MPNum};  %do this to store mission point information that is constant
%RUN THROUGH ALL OF THE ROWS IN VARIABLE_MAT
for i=1:size(Variable_Mat,1)
    %Put the values from the current Variable_Mat row into the
    %TMSDesign.Conditions structure
    for j=1:length(Variable_Names)
        eval(['TMSDesign.Conditions.',Variable_Names{j},'=',num2str(Variable_Mat(i,j)),';']);
    end

     
    %EXECUTE HEATS
    clear TMSTemp;
    TMSDesign.Opt.Model = 'performanceRTToff';
    TMSTemp = HEATS('BaselineTMSConfigNoMissionFile','nocostgoalattain',TMSDesign);
    TMS{i} = TMSTemp{1};
    
    
    %RETURN TO TANK IF NEEDED
    if TMS{i}.MFTV.Outlet{2}.Temp > TMS{i}.Conditions.MaxFuelTemp
        TMS{i}.Opt.Model = 'performanceRTTon';
        TMSRTT = HEATS('BaselineTMSConfigNoMissionFile','nocostgoalattain',TMS{i});
        TMS{i} = TMSRTT{1};
    end
    
    TMS{i}.Conditions.MissionPoint = Conditions{MPNum}.MissionPoint; %Store MissionPoint Name  Not quite sure why it gets erased

end