Code covered by the BSD License  

Highlights from
CTMSIM - an interactive freeway traffic macrosimulator

image thumbnail
from CTMSIM - an interactive freeway traffic macrosimulator by Alex Kurzhanskiy
Freeway traffic simulation based on Asymmetric Cell Transmission Model

ctmGUI(varargin)
function varargout = ctmGUI(varargin)
% CTMGUI M-file for ctmGUI.fig
%      CTMGUI, by itself, creates a new CTMGUI or raises the existing
%      singleton*.
%
%      H = CTMGUI returns the handle to a new CTMGUI or the handle to
%      the existing singleton*.
%
%      CTMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CTMGUI.M with the given input arguments.
%
%      CTMGUI('Property','Value',...) creates a new CTMGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ctmGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ctmGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ctmGUI

% Last Modified by GUIDE v2.5 15-Nov-2006 15:18:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State     = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @ctmGUI_OpeningFcn, ...
                       'gui_OutputFcn',  @ctmGUI_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);


% Our global data structure will be called 'g_ctmGUI'

global g_ctmGUI;

if nargin & isstr(varargin{1})
  gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
  [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
  gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ctmGUI is made visible.
function ctmGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ctmGUI (see VARARGIN)

% Choose default command line output for ctmGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ctmGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% assign axes attributes

axes(handles.freeway);
axis off;

axes(handles.densityPlot);
xlabel('Post mile');
ylabel('Density (vpm)');

axes(handles.flowPlot);
xlabel('');
ylabel('Flow (vph)');

axes(handles.travelTime);
xlabel('Time (min)');
ylabel('Travel time (min)');


% hide color grades

set(handles.densityCM, 'Visible', 'off');
set(handles.flowCM, 'Visible', 'off')


% initialize our global data structure 'g_ctmGUI'

global g_ctmGUI;

if ~isstruct(g_ctmGUI) | ~isfield(g_ctmGUI, 'configFile')
  g_ctmGUI.configFile = 'ctm';  % default: 'ctm.mat'
end

g_ctmGUI.numColors = 1024;
g_ctmGUI.isStopped = 0;
g_ctmGUI.isSaved   = 1;

load(g_ctmGUI.configFile);

g_ctmGUI.cellData = celldata;  % MUST (!) be present in the config file

if ~exist('TS', 'var')
  TS = 1;
end
minl        = min(get_cell_lengths(g_ctmGUI.cellData));
maxv        = max(get_ff_speeds(g_ctmGUI.cellData));
g_ctmGUI.TS = min([TS ((minl / maxv) - eps)]);  % sampling period

if ~exist('plotTS', 'var')
  plotTS = 1/60;  % plote once a minute
end
g_ctmGUI.plotPeriod = max([round(plotTS/g_ctmGUI.TS) 1]);

if ~exist('dataFile', 'var')
  dataFile = [g_ctmGUI.configFile '_data'];
end
g_ctmGUI.dataFile = dataFile;

if ~exist('taxislim', 'var')
  taxislim = 100;  % time axis limit
end
g_ctmGUI.taxislim = taxislim;

if ~exist('timeout', 'var')
  timeout = 2;  % in seconds
end
g_ctmGUI.timeout = timeout;

if ~exist('yoColorRatio', 'var')
  yoColorRatio = [0.95 0.95];
end
g_ctmGUI.yoColorRatio = yoColorRatio;

if ~exist('densityCMF', 'var')
  densityCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.densityCMF = densityCMF;

if ~exist('flowCMF', 'var')
  flowCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.flowCMF = flowCMF;

if ~exist('orflowCMF', 'var')
  orflowCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.orflowCMF = orflowCMF;

if ~exist('orqueueCMF', 'var')
  orqueueCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.orqueueCMF = orqueueCMF;

if ~exist('frflowCMF', 'var')
  frflowCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.frflowCMF = frflowCMF;

if ~exist('frbetaCMF', 'var')
  frbetaCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.frbetaCMF = frbetaCMF;

if ~exist('speedCMF', 'var')
  speedCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.speedCMF = speedCMF;

if ~exist('vhtCMF', 'var')
  vhtCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.vhtCMF = vhtCMF;

if ~exist('vmtCMF', 'var')
  vmtCMF = jet(g_ctmGUI.numColors);
end
g_ctmGUI.vmtCMF = vmtCMF;

if ~exist('initialDensities', 'var')
  initialDensities = get_critical_densities(g_ctmGUI.cellData);
end
g_ctmGUI.densities = initialDensities;

if ~exist('inflow', 'var')
  inflow = g_ctmGUI.cellData(1).FDfmax;
end
g_ctmGUI.inflow = inflow;

if ~exist('ifKnob', 'var')
  ifKnob = 1;
end
g_ctmGUI.ifKnob = ifKnob;

if ~exist('outflow', 'var')
  outflow = g_ctmGUI.cellData(end).FDfmax;
end
g_ctmGUI.outflow = outflow;

if ~exist('ofKnob', 'var')
  ofKnob = 1;
end
g_ctmGUI.ofKnob = ofKnob;

if ~exist('demandProfile', 'var')
  demandProfile = [];
end
g_ctmGUI.demandProfile = demandProfile;

if ~exist('demandTS', 'var')
  demandTS = 1/12;  % 5 minutes
end
g_ctmGUI.demandPeriod = round(demandTS/TS);
g_ctmGUI.demandIndex  = 0;
g_ctmGUI.demandCount  = 0;

if ~exist('betaProfile', 'var')
  betaProfile = [];
end
g_ctmGUI.betaProfile = betaProfile;

if ~exist('betaTS', 'var')
  betaTS = 1/12;  % 5 minutes
end
g_ctmGUI.betaPeriod = round(betaTS/TS);
g_ctmGUI.betaIndex  = 0;
g_ctmGUI.betaCount  = 0;

if ~exist('frflowProfile', 'var')
  frflowProfile = [];
end
g_ctmGUI.frflowProfile = frflowProfile;

if ~exist('frflowTS', 'var')
  frflowTS = 1/12;  % 5 minutes
end
g_ctmGUI.frflowPeriod = round(frflowTS/TS);
g_ctmGUI.frflowIndex  = 0;
g_ctmGUI.frflowCount  = 0;

if ~exist('maxSimStep', 'var')
  maxSimStep = Inf;
end
g_ctmGUI.maxSimStep = maxSimStep;

if ~exist('maxSimTime', 'var')
  maxSimTime = Inf;
end
g_ctmGUI.maxSimTime = maxSimTime;

g_ctmGUI.densityData    = [];
g_ctmGUI.flowData       = [];
g_ctmGUI.speedData      = [];
g_ctmGUI.demandData     = [];
g_ctmGUI.orflowData     = [];
g_ctmGUI.orqueueData    = [];
g_ctmGUI.frflowData     = [];
g_ctmGUI.frbetaData     = [];
g_ctmGUI.vhtData        = [];
g_ctmGUI.vmtData        = [];
g_ctmGUI.delayData      = [];
g_ctmGUI.plossData      = [];
g_ctmGUI.traveltimeData = [];
g_ctmGUI.tvhData        = [];
g_ctmGUI.tvmData        = [];
g_ctmGUI.cdelayData     = [];
g_ctmGUI.cplossData      = [];
g_ctmGUI.timeMinutes    = [];  % data needed for the TIME axis
g_ctmGUI.timeStep       = 0;
g_ctmGUI.linIndex       = 0;
g_ctmGUI.loutIndex      = 1;

[g_ctmGUI.maxDensity, g_ctmGUI.maxFlow] = get_max_df(g_ctmGUI.cellData);
if exist('maxFlow', 'var')
  g_ctmGUI.maxFlow = maxFlow;
end
if exist('maxDensity', 'var')
  g_ctmGUI.maxDensity = maxDensity;
end
if exist('minFlow', 'var')
  g_ctmGUI.minFlow = minFlow;
else
  g_ctmGUI.minFlow = 0;
end
if exist('minDensity', 'var')
  g_ctmGUI.minDensity = minDensity;
else
  g_ctmGUI.minDensity = 0;
end

g_ctmGUI.pmData = get_pm_data(g_ctmGUI.cellData);



% specify the freeway and direction

if ~exist('freeway', 'var')
  freeway = 'Freeway XYZ';
end
g_ctmGUI.freeway = freeway;
set(handles.header, 'String', freeway);

axes(handles.direction);
if g_ctmGUI.cellData(1).PMstart <= g_ctmGUI.cellData(1).PMend
  image(imread('lr.jpg'));
else
  image(imread('rl.jpg'));
end
axis off;


% populate on- and off- ramp lists

[orls, frls] = get_ramp_lists(g_ctmGUI.cellData);
set(handles.onrampList, 'String', orls);
set(handles.offrampList, 'String', frls);

set(handles.flowText, 'String', num2str(g_ctmGUI.inflow));
set(handles.flowSlider, 'Max', g_ctmGUI.maxFlow);
set(handles.flowSlider, 'Value', g_ctmGUI.inflow);

if isempty(g_ctmGUI.cellData(1).FRname)
  set(handles.splitratioText, 'String', '');
  set(handles.splitratioSlider, 'Value', 0);
  set(handles.splitratioText, 'Enable', 'off');
  set(handles.splitratioSlider, 'Enable', 'off');
else
  set(handles.splitratioText, 'Enable', 'on');
  set(handles.splitratioSlider, 'Enable', 'on');
  set(handles.splitratioText, 'String', num2str(g_ctmGUI.cellData(1).FRbeta));
  set(handles.splitratioSlider, 'Value', g_ctmGUI.cellData(1).FRbeta);
end


% get X axis limits

xmin = g_ctmGUI.pmData(1);
xmax = g_ctmGUI.pmData(end);
if xmin > xmax
  xlim = [xmax xmin];
else
  xlim = [xmin xmax];
end
g_ctmGUI.xLims = xlim;


% initialize axes

axes(handles.densityPlot);
axis([g_ctmGUI.xLims 0 1]);

axes(handles.flowPlot);
axis([g_ctmGUI.xLims 0 1]);

axes(handles.freeway);
plotbar(g_ctmGUI.pmData, 0.5*ones(1, size(g_ctmGUI.cellData, 2)), 'w');
hold on;
idx = g_ctmGUI.linIndex;
if (idx > 0) & (idx <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(idx), 0.75, 'bv', 'MarkerFaceColor', 'b');
end
idx = g_ctmGUI.loutIndex;
plot(g_ctmGUI.pmData(idx+1), 0.75, 'c^', 'MarkerFaceColor', 'c');
axis([g_ctmGUI.xLims 0 1]);
set(handles.freeway, 'YTick', []);
hold off;


% Auto menu items

if isempty(g_ctmGUI.demandProfile)
  set(handles.menuAutoDemand, 'Enable', 'off');
end

if isempty(g_ctmGUI.betaProfile)
  set(handles.menuAutoBeta, 'Enable', 'off');
end

if isempty(g_ctmGUI.frflowProfile)
  set(handles.menuAutoFRFlow, 'Enable', 'off');
end

set(handles.menuAutoControl, 'Enable', 'off');
set(handles.menuAutoQControl, 'Enable', 'off');


% Save initial betas and on-ramp flows
g_ctmGUI.frbetas = get_fr_splitratios(g_ctmGUI.cellData)';
g_ctmGUI.orflows = get_or_flows(g_ctmGUI.cellData)';

return;





% --- Outputs from this function are returned to the command line.
function varargout = ctmGUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

return;





% --- Executes during object creation, after setting all properties.
function onrampList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to onrampList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

% --- Executes on selection change in onrampList.
function onrampList_Callback(hObject, eventdata, handles)
% hObject    handle to onrampList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns onrampList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from onrampList

global g_ctmGUI;

indx = get(hObject, 'Value') - 1;  % index of the selected item 

if indx == 0
  val = g_ctmGUI.inflow;
  set(handles.flowSlider, 'Max', max(get_max_flows(g_ctmGUI.cellData)));
  set(handles.flowText, 'String', num2str(val));
  set(handles.flowSlider, 'Value', val);
  set(handles.flowText, 'Enable', 'on');
  set(handles.flowSlider, 'Enable', 'on');
elseif indx > size(g_ctmGUI.cellData, 2)
  val = g_ctmGUI.outflow;
  set(handles.flowSlider, 'Max', max(get_max_flows(g_ctmGUI.cellData)));
  set(handles.flowText, 'String', num2str(val));
  set(handles.flowSlider, 'Value', val);
  set(handles.flowText, 'Enable', 'on');
  set(handles.flowSlider, 'Enable', 'on');
else
  val = g_ctmGUI.cellData(indx).ORflow;
  if isempty(g_ctmGUI.cellData(indx).ORname)
    set(handles.flowText, 'String', '');
    set(handles.flowSlider, 'Max', 1);
    set(handles.flowSlider, 'Value', 0);
    set(handles.flowText, 'Enable', 'off');
    set(handles.flowSlider, 'Enable', 'off');
  else
    set(handles.flowText, 'Enable', 'on');
    set(handles.flowSlider, 'Enable', 'on');
    set(handles.flowText, 'String', num2str(val));
    set(handles.flowSlider, 'Max', g_ctmGUI.cellData(indx).ORfmax);
    set(handles.flowSlider, 'Value', val);
  end
end

axes(handles.freeway);
hold on;
if (g_ctmGUI.linIndex > 0) & (g_ctmGUI.linIndex <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(g_ctmGUI.linIndex), 0.75, 'wv', 'MarkerFaceColor', 'w');
end
plot(g_ctmGUI.pmData(g_ctmGUI.loutIndex+1), 0.75, 'w^', 'MarkerFaceColor', 'w');
plot(g_ctmGUI.pmData(g_ctmGUI.loutIndex+1), 0.75, 'c^', 'MarkerFaceColor', 'c');
if (indx > 0) & (indx <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(indx), 0.75, 'bv', 'MarkerFaceColor', 'b');
end
axis([g_ctmGUI.xLims 0 1]);
set(handles.freeway, 'YTick', []);
hold off;

g_ctmGUI.linIndex = indx;

return;





% --- Executes during object creation, after setting all properties.
function flowText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to flowText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

function flowText_Callback(hObject, eventdata, handles)
% hObject    handle to flowText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of flowText as text
%        str2double(get(hObject,'String')) returns contents of flowText as a double

global g_ctmGUI;

indx = get(handles.onrampList, 'Value') - 1;  % index of current on-ramp
val  = str2double(get(hObject, 'String'));

if ~(val > 0)
  val = 0;
end

if indx == 0
  g_ctmGUI.inflow = val;  % set new value for in-flow
elseif indx > size(g_ctmGUI.cellData, 2)
  g_ctmGUI.outflow = val;  % set new value for out-flow
else
  if ~isempty(g_ctmGUI.cellData(indx).ORname)
    if val > g_ctmGUI.cellData(indx).ORfmax
      val = g_ctmGUI.cellData(indx).ORfmax;
    end
    g_ctmGUI.cellData(indx).ORflow = val;  % set new value for current on-ramp flow
    set(handles.flowSlider, 'Value', val);
    g_ctmGUI.isSaved = 0;
  else
    set(handles.flowText, 'String', '');
  end
end

return;





% --- Executes during object creation, after setting all properties.
function flowSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to flowSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.

% --- Executes on slider movement.
function flowSlider_Callback(hObject, eventdata, handles)
% hObject    handle to flowSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global g_ctmGUI;

indx = get(handles.onrampList, 'Value') - 1;  % index of current on-ramp
val  = get(hObject, 'Value');

if indx == 0
  g_ctmGUI.inflow = val;  % set new value for in-flow
  set(handles.flowText, 'String', num2str(val));
elseif indx > size(g_ctmGUI.cellData, 2)
  g_ctmGUI.outflow = val;  % set new value for out-flow
  set(handles.flowText, 'String', num2str(val));
else
  if ~isempty(g_ctmGUI.cellData(indx).ORname)
    g_ctmGUI.cellData(indx).ORflow = val;  % set new value for current on-ramp flow
    set(handles.flowText, 'String', num2str(val));
    g_ctmGUI.isSaved = 0;
  else
    set(handles.flowSlider, 'Value', 0);
  end
end

return;





% --- Executes during object creation, after setting all properties.
function offrampList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to offrampList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

% --- Executes on selection change in offrampList.
function offrampList_Callback(hObject, eventdata, handles)
% hObject    handle to offrampList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns offrampList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from offrampList

global g_ctmGUI;

indx = get(hObject, 'Value');  % index of the selected item 
val  = g_ctmGUI.cellData(indx).FRbeta;

if isempty(g_ctmGUI.cellData(indx).FRname)
  set(handles.splitratioText, 'String', '');
  set(handles.splitratioSlider, 'Value', 0);
  set(handles.splitratioText, 'Enable', 'off');
  set(handles.splitratioSlider, 'Enable', 'off');
else
  set(handles.splitratioText, 'Enable', 'on');
  set(handles.splitratioSlider, 'Enable', 'on');
  set(handles.splitratioText, 'String', num2str(val));
  set(handles.splitratioSlider, 'Value', val);
end

axes(handles.freeway);
hold on;
if (g_ctmGUI.linIndex > 0) & (g_ctmGUI.linIndex <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(g_ctmGUI.linIndex), 0.75, 'wv', 'MarkerFaceColor', 'w');
end
plot(g_ctmGUI.pmData(g_ctmGUI.loutIndex+1), 0.75, 'w^', 'MarkerFaceColor', 'w');
if (g_ctmGUI.linIndex > 0) & (g_ctmGUI.linIndex <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(g_ctmGUI.linIndex), 0.75, 'bv', 'MarkerFaceColor', 'b');
end
plot(g_ctmGUI.pmData(indx+1), 0.75, 'c^', 'MarkerFaceColor', 'c');
axis([g_ctmGUI.xLims 0 1]);
set(handles.freeway, 'YTick', []);
hold off;

g_ctmGUI.loutIndex = indx;

return;





% --- Executes during object creation, after setting all properties.
function splitratioText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to splitratioText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

function splitratioText_Callback(hObject, eventdata, handles)
% hObject    handle to splitratioText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of splitratioText as text
%        str2double(get(hObject,'String')) returns contents of splitratioText as a double

global g_ctmGUI;

indx = get(handles.offrampList, 'Value');  % index of current off-ramp
val  = str2double(get(hObject, 'String'));

if ~(val > 0)
  val = 0;
end

if val > 1
  val = 1;
end

if ~isempty(g_ctmGUI.cellData(indx).FRname)
  g_ctmGUI.cellData(indx).FRbeta = val;  % set new value for current off-ramp split ratio
  set(handles.splitratioSlider, 'Value', val);
  g_ctmGUI.isSaved = 0;
else
  set(handles.splitratioText, 'String', '');
end

return;





% --- Executes during object creation, after setting all properties.
function splitratioSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to splitratioSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.

% --- Executes on slider movement.
function splitratioSlider_Callback(hObject, eventdata, handles)
% hObject    handle to splitratioSlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

global g_ctmGUI;

indx = get(handles.offrampList, 'Value');  % index of current off-ramp
val  = get(hObject, 'Value');

if ~isempty(g_ctmGUI.cellData(indx).FRname)
  g_ctmGUI.cellData(indx).FRbeta = val;  % set new value for current off-ramp split ratio
  set(handles.splitratioText, 'String', num2str(val));
  g_ctmGUI.isSaved = 0;
else
  set(handles.splitratioSlider, 'Value', 0);
end

return;





% --- Executes on button press in stopButton.
function stopButton_Callback(hObject, eventdata, handles)
% hObject    handle to stopButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped == 0
  g_ctmGUI.isStopped = 1;
else
  beep;
end

return;





% --- Executes on button press in clearButton.
function clearButton_Callback(hObject, eventdata, handles)
% hObject    handle to clearButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

NC = size(g_ctmGUI.cellData, 2);

% reset all the data arrays to empty
if ~isempty(g_ctmGUI.timeMinutes) & (g_ctmGUI.isStopped == 0)
  g_ctmGUI.densityData    = g_ctmGUI.densityData(:, end);
  g_ctmGUI.flowData       = g_ctmGUI.flowData(:, end);
  g_ctmGUI.demandData     = g_ctmGUI.demandData(:, end);
  g_ctmGUI.orflowData     = g_ctmGUI.orflowData(:, end);
  g_ctmGUI.orqueueData    = g_ctmGUI.orqueueData(:, end);
  g_ctmGUI.frflowData     = g_ctmGUI.frflowData(:, end);
  g_ctmGUI.frbetaData     = g_ctmGUI.frbetaData(:, end);
  g_ctmGUI.speedData      = g_ctmGUI.speedData(:, end);
  g_ctmGUI.vhtData        = g_ctmGUI.vhtData(:, end);
  g_ctmGUI.vmtData        = g_ctmGUI.vmtData(:, end);
  g_ctmGUI.delayData      = g_ctmGUI.delayData(:, end);
  g_ctmGUI.plossData      = g_ctmGUI.plossData(:, end);
  g_ctmGUI.cdelayData     = g_ctmGUI.cdelayData(:, end);
  g_ctmGUI.cplossData     = g_ctmGUI.cplossData(:, end);
  g_ctmGUI.traveltimeData = g_ctmGUI.traveltimeData(:, end);
  g_ctmGUI.tvhData        = g_ctmGUI.tvhData(:, end);
  g_ctmGUI.tvmData        = g_ctmGUI.tvmData(:, end);
  g_ctmGUI.timeMinutes    = g_ctmGUI.timeMinutes(:, end);
else
  g_ctmGUI.densityData    = [];
  g_ctmGUI.flowData       = [];
  g_ctmGUI.demandData     = [];
  g_ctmGUI.orflowData     = [];
  g_ctmGUI.orqueueData    = [];
  g_ctmGUI.frflowData     = [];
  g_ctmGUI.frbetaData     = [];
  g_ctmGUI.speedData      = [];
  g_ctmGUI.vhtData        = [];
  g_ctmGUI.vmtData        = [];
  g_ctmGUI.delayData      = [];
  g_ctmGUI.plossData      = [];
  g_ctmGUI.cdelayData     = [];
  g_ctmGUI.cplossData     = [];
  g_ctmGUI.traveltimeData = [];
  g_ctmGUI.tvhData        = [];
  g_ctmGUI.tvmData        = [];
  g_ctmGUI.timeMinutes    = [];
  g_ctmGUI.demandIndex    = 0;
  g_ctmGUI.demandCount    = 0;
  g_ctmGUI.betaIndex      = 0;
  g_ctmGUI.betaCount      = 0;
  g_ctmGUI.frflowIndex    = 0;
  g_ctmGUI.frflowCount    = 0;
  g_ctmGUI.cellData       = set_or_flows(g_ctmGUI.cellData, g_ctmGUI.orflows);
  g_ctmGUI.cellData       = set_fr_split_ratios(g_ctmGUI.cellData, g_ctmGUI.frbetas);
  % update value in the on-ramp flow text box and slider
  indx = get(handles.onrampList, 'Value') - 1;
  if indx == 0
    set(handles.flowText, 'String', num2str(g_ctmGUI.inflow));
    set(handles.flowSlider, 'Value', g_ctmGUI.inflow);
  elseif indx > NC
    set(handles.flowText, 'String', num2str(g_ctmGUI.outflow));
    set(handles.flowSlider, 'Value', g_ctmGUI.outflow);
  else
    if isempty(g_ctmGUI.cellData(indx).ORname)
      set(handles.flowText, 'String', '');
      set(handles.flowSlider, 'Value', 0);
    else
      set(handles.flowText, 'String', num2str(g_ctmGUI.cellData(indx).ORflow));
      set(handles.flowSlider, 'Value', g_ctmGUI.cellData(indx).ORflow);
    end
  end
  % update value in the off-ramp split ratio text box and slider
  indx = get(handles.offrampList, 'Value');
  if isempty(g_ctmGUI.cellData(indx).FRname)
    set(handles.splitratioText, 'String', '');
    set(handles.splitratioSlider, 'Value', 0);
    set(handles.splitratioText, 'Enable', 'off');
    set(handles.splitratioSlider, 'Enable', 'off');
  else
    set(handles.splitratioText, 'Enable', 'on');
    set(handles.splitratioSlider, 'Enable', 'on');
    set(handles.splitratioText, 'String', num2str(g_ctmGUI.cellData(indx).FRbeta));
    set(handles.splitratioSlider, 'Value', g_ctmGUI.cellData(indx).FRbeta);
  end
  % clear global controller structure
  global CTMSIM_ORC;
  clear global CTMSIM_ORC;
end

% clear plots
axes(handles.travelTime);
cla;

if strcmp(get(handles.menuORDisplay, 'Checked'), 'off')
  plot_data_0(handles);
else
  plot_data_10(handles);
end
axes(handles.freeway);
plotbar(g_ctmGUI.pmData, 0.5*ones(1, size(g_ctmGUI.cellData, 2)), 'w');
hold on;
idx = g_ctmGUI.linIndex;
if (idx > 0) & (idx <= size(g_ctmGUI.cellData, 2))
  plot(g_ctmGUI.pmData(idx), 0.75, 'bv', 'MarkerFaceColor', 'b');
end
idx = g_ctmGUI.loutIndex;
plot(g_ctmGUI.pmData(idx+1), 0.75, 'c^', 'MarkerFaceColor', 'c');
axis([g_ctmGUI.xLims 0 1]);
set(handles.freeway, 'YTick', []);
hold off;

return;





% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
% hObject    handle to startButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;


if g_ctmGUI.isStopped < 0  % cannot start, configuration tasks are running
  beep;
  return;
end

g_ctmGUI.isSaved = 0;

NC               = size(g_ctmGUI.cellData, 2);  % number of cells

if isempty(g_ctmGUI.timeMinutes)  % start new graphs
  densities                 = g_ctmGUI.densities;
  orflows                   = get_or_fmax(g_ctmGUI.cellData);
  orflows                   = orflows';
  orqueues                  = zeros(NC, 1);
  g_ctmGUI.controllerCounts = zeros(NC, 1);
  g_ctmGUI.timeStep         = -1;
  flows                     = zeros(NC, 1);
  frflows                   = zeros(NC, 1);
else  % continue where we left off
  densities = g_ctmGUI.densityData(1:NC, end);
  flows     = g_ctmGUI.flowData(1:NC, end);
  orflows   = g_ctmGUI.orflowData(1:NC, end);
  orqueues  = g_ctmGUI.orqueueData(1:NC, end);
  frbetas   = g_ctmGUI.frbetaData(1:NC, end);
  frflows   = g_ctmGUI.frflowData(1:NC, end);
  speeds    = g_ctmGUI.speedData(1:NC, end);
  vht       = g_ctmGUI.vhtData(1:NC, end);
  vmt       = g_ctmGUI.vmtData(1:NC, end);
  delay     = g_ctmGUI.delayData(1:NC, end);
  ploss     = g_ctmGUI.plossData(1:NC, end);
end

g_ctmGUI.controllerPeriods = get_controller_periods(g_ctmGUI.cellData, g_ctmGUI.TS);
g_ctmGUI.isStopped         = 0;

orflows_prev = orflows;
onerow       = ones(1, NC);
timeStep     = g_ctmGUI.timeStep;
count        = -1;

% main loop
while g_ctmGUI.isStopped == 0  % run while STOP button is not pressed

  if (count + 1) == g_ctmGUI.plotPeriod
    count = -1;
  end
  count = count + 1;

  % determine actual on-ramp demands, flows, queues
  if strcmp(get(handles.menuAutoDemand, 'Enable'), 'on') & ...
     strcmp(get(handles.menuAutoDemand, 'Checked'), 'on')
    if g_ctmGUI.demandCount == 0
      g_ctmGUI.demandIndex = g_ctmGUI.demandIndex + 1;
      if g_ctmGUI.demandIndex > size(g_ctmGUI.demandProfile, 1)
        g_ctmGUI.demandIndex = 1;
        g_ctmGUI.isStopped   = 1;
      end
    end
    if (g_ctmGUI.demandCount + 1) == g_ctmGUI.demandPeriod
      g_ctmGUI.demandCount = -1;
    end
    g_ctmGUI.demandCount = g_ctmGUI.demandCount + 1;
    demands              = g_ctmGUI.demandProfile(g_ctmGUI.demandIndex, :);
    g_ctmGUI.inflow      = g_ctmGUI.ifKnob * demands(1);
    g_ctmGUI.outflow     = g_ctmGUI.ofKnob * demands(end);
    demands              = adjust_or_demands(demands(2:(NC+1))', g_ctmGUI.cellData);
    
    if strcmp(get(handles.menuAutoControl, 'Checked'), 'on')
      [orflows, g_ctmGUI.controllerCounts] = ...
         auto_control(densities, ...
                      [demands orflows orqueues], ...
                      g_ctmGUI.controllerCounts, ...
                      g_ctmGUI.controllerPeriods, ...
                      g_ctmGUI.cellData, ...
                      g_ctmGUI.TS, ...
                      strcmp(get(handles.menuAutoQControl, 'Checked'), 'on'));
    else
      orflows = adjust_or_flow_limits(densities, g_ctmGUI.cellData, g_ctmGUI.TS);
    end
  else
    demands  = get_or_flows(g_ctmGUI.cellData);
    demands  = demands';
    orflows  = demands;
  end

  % make sure on-ramp flows do nor exceed allowed limits
  orflows = adjust_or_flows(densities, orflows, g_ctmGUI.cellData, g_ctmGUI.TS);

  % adjust on-ramp flow and queue values
  [orflows, orqueues] = adjust_or_data(demands, ...
                                       orflows, ...
                                       orqueues, ...
                                       g_ctmGUI.TS);

  % update on-ramp flows in cell structures
  g_ctmGUI.cellData   = set_or_flows(g_ctmGUI.cellData, orflows);
  if timeStep == -1
    orflows_prev = orflows;
  end

  % update value in the on-ramp flow text box and slider
  indx = get(handles.onrampList, 'Value') - 1;
  if indx == 0
    set(handles.flowText, 'String', num2str(g_ctmGUI.inflow));
    set(handles.flowSlider, 'Value', g_ctmGUI.inflow);
  elseif indx > NC
    set(handles.flowText, 'String', num2str(g_ctmGUI.outflow));
    set(handles.flowSlider, 'Value', g_ctmGUI.outflow);
  else
    if isempty(g_ctmGUI.cellData(indx).ORname)
      set(handles.flowText, 'String', '');
      set(handles.flowSlider, 'Value', 0);
    else
      set(handles.flowText, 'String', num2str(g_ctmGUI.cellData(indx).ORflow));
      set(handles.flowSlider, 'Value', g_ctmGUI.cellData(indx).ORflow);
    end
  end

  % determine off-ramp split ratio values
  if strcmp(get(handles.menuAutoBeta, 'Enable'), 'on') & ...
     strcmp(get(handles.menuAutoBeta, 'Checked'), 'on')
    if g_ctmGUI.betaCount == 0
      g_ctmGUI.betaIndex = g_ctmGUI.betaIndex + 1;
      if g_ctmGUI.betaIndex > size(g_ctmGUI.betaProfile, 1)
        g_ctmGUI.betaIndex = 1;
        g_ctmGUI.isStopped = 1;
      end
    end
    if (g_ctmGUI.betaCount + 1) == g_ctmGUI.betaPeriod
      g_ctmGUI.betaCount = -1;
    end
    g_ctmGUI.betaCount = g_ctmGUI.betaCount + 1;
    frbetas            = g_ctmGUI.betaProfile(g_ctmGUI.betaIndex, :);
    frbetas            = adjust_fr_split_ratios(frbetas', g_ctmGUI.cellData);
    g_ctmGUI.cellData  = set_fr_split_ratios(g_ctmGUI.cellData, frbetas);
  else
    frbetas            = get_fr_splitratios(g_ctmGUI.cellData);
    frbetas            = frbetas';
    g_ctmGUI.cellData  = set_fr_split_ratios(g_ctmGUI.cellData, frbetas);
  end


  if strcmp(get(handles.menuAutoFRFlow, 'Enable'), 'on') & ...
     strcmp(get(handles.menuAutoFRFlow, 'Checked'), 'on')
    if g_ctmGUI.frflowCount == 0
      g_ctmGUI.frflowIndex = g_ctmGUI.frflowIndex + 1;
      if g_ctmGUI.frflowIndex > size(g_ctmGUI.frflowProfile, 1)
        g_ctmGUI.frflowIndex = 1;
        g_ctmGUI.isStopped = 1;
      end
    end
    if (g_ctmGUI.frflowCount + 1) == g_ctmGUI.frflowPeriod
      g_ctmGUI.frflowCount = -1;
    end
    g_ctmGUI.frflowCount = g_ctmGUI.frflowCount + 1;
    frflows              = g_ctmGUI.frflowProfile(g_ctmGUI.frflowIndex, :);
    frflows              = adjust_fr_flows(densities, ...
                                           frflows', ...
                                           g_ctmGUI.cellData, ...
                                           g_ctmGUI.TS);

    [simdata, traveltime] = simulation_step_2(g_ctmGUI.cellData, ...
                                              densities, ...
                                              orflows_prev, ...
                                              orqueues, ...
                                              flows, ...
                                              frflows, ...
                                              g_ctmGUI.inflow, ...
                                              g_ctmGUI.outflow, ...
                                              g_ctmGUI.TS, ...
                                              timeStep);
    frbetas               = simdata(:, 3);

    % update beta values in the cell structures
    g_ctmGUI.cellData     = set_fr_split_ratios(g_ctmGUI.cellData, frbetas);
  else
    [simdata, traveltime] = simulation_step(g_ctmGUI.cellData, ...
                                            densities, ...
                                            orflows_prev, ...
                                            orqueues, ...
                                            flows, ...
                                            frflows, ...
                                            g_ctmGUI.inflow, ...
                                            g_ctmGUI.outflow, ...
                                            g_ctmGUI.TS, ...
                                            timeStep);
    frflows               = simdata(:, 3);

    % get off-ramp split ratios
    frbetas               = get_fr_splitratios(g_ctmGUI.cellData);
    frbetas               = frbetas';
  end

  densities    = simdata(:, 1);
  flows        = simdata(:, 2);
  speeds       = simdata(:, 4);
  orflows_prev = orflows;

  % compute Vehicle Hours Traveled (VHT), Vehicle Miles Traveled (VMT)
  % and Productivity Loss
  if timeStep >= 0
    vht   = vht + simdata(:, 5);
    vmt   = vmt + simdata(:, 6);
    ploss = ploss + simdata(:, 7);
  else
    vht   = g_ctmGUI.plotPeriod * simdata(:, 5);
    vmt   = g_ctmGUI.plotPeriod * simdata(:, 6);
    ploss = g_ctmGUI.plotPeriod * simdata(:, 7);
  end

  % compute total Productivity Loss
  cploss    = sum(ploss);

  % compute new Total Vehicle Hours
  tvh        = sum(vht);

  % compute new Total Vehicle Miles
  tvm        = sum(vmt);

  % compute delay
  delay      = vht  -  (vmt ./ get_ff_speeds(g_ctmGUI.cellData));
  delay      = max([delay'; zeros(1, NC)])';
  cdelay     = sum(delay);

  % next time step
  timeStep   = timeStep + 1;

  % update value in the off-ramp split ratio text box and slider
  indx = get(handles.offrampList, 'Value');
  if isempty(g_ctmGUI.cellData(indx).FRname)
    set(handles.splitratioText, 'String', '');
    set(handles.splitratioSlider, 'Value', 0);
    set(handles.splitratioText, 'Enable', 'off');
    set(handles.splitratioSlider, 'Enable', 'off');
  else
    set(handles.splitratioText, 'Enable', 'on');
    set(handles.splitratioSlider, 'Enable', 'on');
    set(handles.splitratioText, 'String', num2str(g_ctmGUI.cellData(indx).FRbeta));
    set(handles.splitratioSlider, 'Value', g_ctmGUI.cellData(indx).FRbeta);
  end


  if count > 0
    continue;
  end


  % update data arrays
  g_ctmGUI.timeStep       = timeStep;
  tm                      = g_ctmGUI.timeStep * g_ctmGUI.TS * 60;
  g_ctmGUI.timeMinutes    = [g_ctmGUI.timeMinutes tm];
  g_ctmGUI.densityData    = [g_ctmGUI.densityData [densities; densities(end, 1)]];
  g_ctmGUI.flowData       = [g_ctmGUI.flowData [flows; flows(end, 1)]];
  g_ctmGUI.demandData     = [g_ctmGUI.demandData [demands; demands(end, 1)]];
  g_ctmGUI.orflowData     = [g_ctmGUI.orflowData [orflows; orflows(end, 1)]];
  g_ctmGUI.orqueueData    = [g_ctmGUI.orqueueData [orqueues; 200]];
  g_ctmGUI.frflowData     = [g_ctmGUI.frflowData [frflows; frflows(end, 1)]];
  g_ctmGUI.frbetaData     = [g_ctmGUI.frbetaData [frbetas; frbetas(end, 1)]];
  g_ctmGUI.speedData      = [g_ctmGUI.speedData [speeds; speeds(end, 1)]];
  g_ctmGUI.vhtData        = [g_ctmGUI.vhtData [vht; vht(end, 1)]];
  g_ctmGUI.vmtData        = [g_ctmGUI.vmtData [vmt; vmt(end, 1)]];
  g_ctmGUI.delayData      = [g_ctmGUI.delayData [delay; delay(end, 1)]];
  g_ctmGUI.plossData      = [g_ctmGUI.plossData [ploss; ploss(end, 1)]];
  g_ctmGUI.traveltimeData = [g_ctmGUI.traveltimeData traveltime];
  g_ctmGUI.tvhData        = [g_ctmGUI.tvhData tvh];
  g_ctmGUI.tvmData        = [g_ctmGUI.tvmData tvm];
  g_ctmGUI.cdelayData     = [g_ctmGUI.cdelayData cdelay];
  g_ctmGUI.cplossData     = [g_ctmGUI.cplossData cploss];

  vht                     = zeros(NC, 1);
  vmt                     = zeros(NC, 1);
  ploss                   = zeros(NC, 1);

 
  % plot data
  if strcmp(get(handles.menuViewVHTVMT, 'Checked'), 'on')
    plot_data_31(handles);
  elseif strcmp(get(handles.menuFRDisplay, 'Checked'), 'on')
    plot_data_21(handles);
  elseif strcmp(get(handles.menuORDisplay, 'Checked'), 'on')
    plot_data_11(handles, demands);
  else
    plot_data_1(handles);
  end

  plot_data_T(handles);


  % sleep for specified number of seconds
  if g_ctmGUI.timeout > 0
    pause(g_ctmGUI.timeout);
  end

end


return;





% --------------------------------------------------------------------
function menuFile_Callback(hObject, eventdata, handles)
% hObject    handle to menuFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuLoad_Callback(hObject, eventdata, handles)
% hObject    handle to menuLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  currFig = get(0, 'CurrentFigure');
  if g_ctmGUI.isSaved == 0
    pos_size = get(currFig, 'Position');
    res      = save_dialog([pos_size(1)+pos_size(3)/2.5 pos_size(2)+pos_size(4)/2.5], 'Save simulation?');
    if res == 1  % save simulation
      fn = uiputfile('*.mat');
      if ~isa(fn, 'double')
        g_ctmGUI.isSaved = 1;
        ctmGUI_data      = g_ctmGUI;
        if str2num(version('-release')) >= 14
          feval(@save, '-v6', fn, 'ctmGUI_data');
        else
          feval(@save, fn, 'ctmGUI_data');
        end
      end
    elseif res == 2
      return;
    end
  end

  g_ctmGUI.isStopped = -1;
  fn                 = uigetfile('*.mat');
  if ~isa(fn, 'double')
    load(fn);
    if exist('ctmGUI_data', 'var')
      g_ctmGUI = ctmGUI_data;

      [orls, frls] = get_ramp_lists(g_ctmGUI.cellData);
      set(handles.onrampList, 'String', orls);
      set(handles.offrampList, 'String', frls);
      set(handles.onrampList, 'Value', 1);
      set(handles.flowText, 'String', num2str(g_ctmGUI.inflow));
      set(handles.flowSlider, 'Max', g_ctmGUI.maxFlow);
      set(handles.flowSlider, 'Value', g_ctmGUI.inflow);
      set(handles.offrampList, 'Value', 1);
      if isempty(g_ctmGUI.cellData(1).FRname)
        set(handles.splitratioText, 'String', '');
        set(handles.splitratioSlider, 'Value', 0);
        set(handles.splitratioText, 'Enable', 'off');
        set(handles.splitratioSlider, 'Enable', 'off');
      else
        set(handles.splitratioText, 'Enable', 'on');
        set(handles.splitratioSlider, 'Enable', 'on');
        set(handles.splitratioText, 'String', num2str(g_ctmGUI.cellData(1).FRbeta));
        set(handles.splitratioSlider, 'Value', g_ctmGUI.cellData(1).FRbeta);
      end
      g_ctmGUI.linIndex  = 0;
      g_ctmGUI.loutIndex = 1;

      if ~isempty(g_ctmGUI.timeMinutes)
        if strcmp(get(handles.menuViewVHTVMT, 'Checked'), 'on')
          plot_data_31(handles);
	elseif strcmp(get(handles.menuFRDisplay, 'Checked'), 'on')
          plot_data_21(handles);
        elseif strcmp(get(handles.menuORDisplay, 'Checked'), 'on')
          plot_data_11(handles);
        else
          plot_data_1(handles);
        end
      else
        if strcmp(get(handles.menuViewVHTVMT, 'Checked'), 'on')
          plot_data_30(handles);
	elseif strcmp(get(handles.menuFRDisplay, 'Checked'), 'on')
          plot_data_20(handles);
        elseif strcmp(get(handles.menuORDisplay, 'Checked'), 'on')
          plot_data_10(handles);
        else
          plot_data_0(handles);
        end
        axes(handles.freeway);
        plotbar(g_ctmGUI.pmData, 0.5*ones(1, size(g_ctmGUI.cellData, 2)), 'w');
        hold on;
        idx = g_ctmGUI.linIndex;
        if (idx > 0) & (idx <= size(g_ctmGUI.cellData, 2))
          plot(g_ctmGUI.pmData(idx), 0.75, 'bv', 'MarkerFaceColor', 'b');
        end
        idx = g_ctmGUI.loutIndex;
        plot(g_ctmGUI.pmData(idx+1), 0.75, 'c^', 'MarkerFaceColor', 'c');
        axis([g_ctmGUI.xLims 0 1]);
        set(handles.freeway, 'YTick', []);
        hold off;
      end

      plot_data_T(handles);

      axes(handles.direction);
      if g_ctmGUI.cellData(1).PMstart <= g_ctmGUI.cellData(1).PMend
        image(imread('lr.jpg'));
      else
        image(imread('rl.jpg'));
      end
      axis off;
      
      if ~isfield(g_ctmGUI, 'demandProfile')
        g_ctmGUI.demandProfile = [];
      end

      if ~isfield(g_ctmGUI, 'betaProfile')
        g_ctmGUI.betaProfile = [];
      end

      if isempty(g_ctmGUI.demandProfile)
        set(handles.menuAutoDemand, 'Enable', 'off');
        set(handles.menuAutoControl, 'Enable', 'off');
        set(handles.menuAutoQControl, 'Enable', 'off');
      else
        set(handles.menuAutoDemand, 'Enable', 'on');
        if strcmp(get(handles.menuAutoDemand, 'Checked'), 'on')
          set(handles.menuAutoControl, 'Enable', 'on');
          if strcmp(get(handles.menuAutoControl, 'Checked'), 'on')
            set(handles.menuAutoQControl, 'Enable', 'on');
          else
            set(handles.menuAutoQControl, 'Enable', 'off');
          end
        else
          set(handles.menuAutoControl, 'Enable', 'off');
          set(handles.menuAutoQControl, 'Enable', 'off');
        end
      end

      if isempty(g_ctmGUI.betaProfile)
        set(handles.menuAutoBeta, 'Checked', 'off');
        set(handles.menuAutoBeta, 'Enable', 'off');
      else
        set(handles.menuAutoBeta, 'Enable', 'on');
      end

      if isempty(g_ctmGUI.frflowProfile)
        set(handles.menuAutoFRFlow, 'Checked', 'off');
        set(handles.menuAutoFRFlow, 'Enable', 'off');
      else
        set(handles.menuAutoFRFlow, 'Enable', 'on');
      end
    end
  end
  g_ctmGUI.isStopped = 1;
else
  beep;
end

return;





% --------------------------------------------------------------------
function menuSave_Callback(hObject, eventdata, handles)
% hObject    handle to menuSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  fn                 = uiputfile('*.mat');
  if ~isa(fn, 'double')
    g_ctmGUI.isSaved = 1;
    ctmGUI_data      = g_ctmGUI;
    if str2num(version('-release')) >= 14
      feval(@save, '-v6', fn, 'ctmGUI_data');
    else
      feval(@save, fn, 'ctmGUI_data');
    end
  end
  g_ctmGUI.isStopped = 1;
else
  beep;
end

return;





% --------------------------------------------------------------------
function menuExit_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ctmGUIclose;

return;





% --------------------------------------------------------------------
function menuEdit_Callback(hObject, eventdata, handles)
% hObject    handle to menuEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuFDButton_Callback(hObject, eventdata, handles)
% hObject    handle to menuFDButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  fdGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuRDButton_Callback(hObject, eventdata, handles)
% hObject    handle to menuRDButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  rdGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuOR_Callback(hObject, eventdata, handles)
% hObject    handle to menuOR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuORControl_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  ctrlGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuORParam_Callback(hObject, eventdata, handles)
% hObject    handle to menuORParam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  orpGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuFR_Callback(hObject, eventdata, handles)
% hObject    handle to menuFR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuFRParam_Callback(hObject, eventdata, handles)
% hObject    handle to menuFRParam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  frpGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuIOF_Callback(hObject, eventdata, handles)
% hObject    handle to menuIOF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuIOFParam_Callback(hObject, eventdata, handles)
% hObject    handle to menuIOFParam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  iofpGUI;
else
  beep;
end

return;



% --------------------------------------------------------------------
function menuEditSettings_Callback(hObject, eventdata, handles)
% hObject    handle to menuEditSettings (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  prmGUI;
else
  beep;
end

return;






% --------------------------------------------------------------------
function menuView_Callback(hObject, eventdata, handles)
% hObject    handle to menuView (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuContoursOn_Callback(hObject, eventdata, handles)
% hObject    handle to menuContoursOn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off')
else
  set(hObject, 'Checked', 'on')
end

if strcmp(get(handles.menuViewVHTVMT, 'Checked'), 'on')
  plot_data_30(handles);
elseif strcmp(get(handles.menuFRDisplay, 'Checked'), 'on')
  plot_data_20(handles);
elseif strcmp(get(handles.menuORDisplay, 'Checked'), 'on')
  plot_data_10(handles);
else
  plot_data_0(handles);
end

if ~isempty(g_ctmGUI.timeMinutes)
  if strcmp(get(handles.menuViewVHTVMT, 'Checked'), 'on')
    plot_data_31(handles);
  elseif strcmp(get(handles.menuFRDisplay, 'Checked'), 'on')
    plot_data_21(handles);
  elseif strcmp(get(handles.menuORDisplay, 'Checked'), 'on')
    plot_data_11(handles);
  else
    plot_data_1(handles);
  end
end

return;



% --------------------------------------------------------------------
function menuSpeedOn_Callback(hObject, eventdata, handles)
% hObject    handle to menuSpeedOn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off')
else
  set(hObject, 'Checked', 'on')
end

plot_data_0(handles);

if ~isempty(g_ctmGUI.timeMinutes)
  plot_data_1(handles);
end

return;



% --------------------------------------------------------------------
function menuORDisplay_Callback(hObject, eventdata, handles)
% hObject    handle to menuORDisplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'on');
  plot_data_0(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_1(handles);
  end
else
  set(hObject, 'Checked', 'on');
  set(handles.menuFRDisplay, 'Checked', 'off');
  set(handles.menuViewVHTVMT, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'off');
  plot_data_10(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_11(handles);
  end
end

return;



% --------------------------------------------------------------------
function menuFRDisplay_Callback(hObject, eventdata, handles)
% hObject    handle to menuFRDisplay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'on');
  plot_data_0(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_1(handles);
  end
else
  set(hObject, 'Checked', 'on');
  set(handles.menuORDisplay, 'Checked', 'off');
  set(handles.menuViewVHTVMT, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'off');
  plot_data_20(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_21(handles);
  end
end

return;



% --------------------------------------------------------------------
function menuViewVHTVMT_Callback(hObject, eventdata, handles)
% hObject    handle to menuViewVHTVMT (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'on');
  plot_data_0(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_1(handles);
  end
else
  set(hObject, 'Checked', 'on');
  set(handles.menuORDisplay, 'Checked', 'off');
  set(handles.menuFRDisplay, 'Checked', 'off');
  set(handles.menuSpeedOn, 'Enable', 'off');
  plot_data_30(handles);
  if ~isempty(g_ctmGUI.timeMinutes)
    plot_data_31(handles);
  end
end

return;



% --------------------------------------------------------------------
function menuViewTVH_Callback(hObject, eventdata, handles)
% hObject    handle to menuViewTVH (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'off')
  set(hObject, 'Checked', 'on');
  set(handles.menuViewTVM, 'Checked', 'off');
  set(handles.menuViewDelay, 'Checked', 'off');
  set(handles.menuViewPLoss, 'Checked', 'off');
else
  set(hObject, 'Checked', 'off');
end

plot_data_T(handles);

return;



% --------------------------------------------------------------------
function menuViewTVM_Callback(hObject, eventdata, handles)
% hObject    handle to menuViewTVM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'off')
  set(hObject, 'Checked', 'on');
  set(handles.menuViewTVH, 'Checked', 'off');
  set(handles.menuViewDelay, 'Checked', 'off');
  set(handles.menuViewPLoss, 'Checked', 'off');
else
  set(hObject, 'Checked', 'off');
end

plot_data_T(handles);

return;



% --------------------------------------------------------------------
function menuViewDelay_Callback(hObject, eventdata, handles)
% hObject    handle to menuViewDelay (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'off')
  set(hObject, 'Checked', 'on');
  set(handles.menuViewTVH, 'Checked', 'off');
  set(handles.menuViewTVM, 'Checked', 'off');
  set(handles.menuViewPLoss, 'Checked', 'off');
else
  set(hObject, 'Checked', 'off');
end

plot_data_T(handles);

return;



% --------------------------------------------------------------------
function menuViewPLoss_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'off')
  set(hObject, 'Checked', 'on');
  set(handles.menuViewTVH, 'Checked', 'off');
  set(handles.menuViewTVM, 'Checked', 'off');
  set(handles.menuViewDelay, 'Checked', 'off');
else
  set(hObject, 'Checked', 'off');
end

plot_data_T(handles);

return;





% --------------------------------------------------------------------
function menuHelp_Callback(hObject, eventdata, handles)
% hObject    handle to menuHelp (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuAbout_Callback(hObject, eventdata, handles)
% hObject    handle to menuAbout (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if g_ctmGUI.isStopped >= 0
  g_ctmGUI.isStopped = -1;
  aboutGUI;
else
  beep;
end

return;





% --------------------------------------------------------------------
function menuAuto_Callback(hObject, eventdata, handles)
% hObject    handle to menuAuto (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuAutoDemand_Callback(hObject, eventdata, handles)
% hObject    handle to menuAutoDemand (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')  % turn off auto demand
  set(hObject, 'Checked', 'off');
  set(handles.menuAutoControl, 'Enable', 'off');
  set(handles.menuAutoQControl, 'Enable', 'off');
  g_ctmGUI.demandIndex = 0;
  g_ctmGUI.demandCount = 0;
else
  set(hObject, 'Checked', 'on');
  set(handles.menuAutoControl, 'Enable', 'on');
  if strcmp(get(handles.menuAutoControl, 'Checked'), 'on')
    set(handles.menuAutoQControl, 'Enable', 'on');
  else
    set(handles.menuAutoQControl, 'Enable', 'off');
  end
end

return;



% --------------------------------------------------------------------
function menuAutoControl_Callback(hObject, eventdata, handles)
% hObject    handle to menuAutoControl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  set(handles.menuAutoQControl, 'Enable', 'off');
else
  set(hObject, 'Checked', 'on');
  set(handles.menuAutoQControl, 'Enable', 'on');
end

return;



% --------------------------------------------------------------------
function menuAutoQControl_Callback(hObject, eventdata, handles)
% hObject    handle to menuAutoQControl (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
else
  set(hObject, 'Checked', 'on');
end

return;



% --------------------------------------------------------------------
function menuAutoBeta_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  g_ctmGUI.betaIndex = 0;
  g_ctmGUI.betaCount = 0;
else
  set(hObject, 'Checked', 'on');
  set(handles.menuAutoFRFlow, 'Checked', 'off');
  g_ctmGUI.frflowIndex = 0;
  g_ctmGUI.frflowCount = 0;
end

return;



% --------------------------------------------------------------------
function menuAutoFRFlow_Callback(hObject, eventdata, handles)
% hObject    handle to menuAutoFRFlow (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global g_ctmGUI;

if strcmp(get(hObject, 'Checked'), 'on')
  set(hObject, 'Checked', 'off');
  g_ctmGUI.frflowIndex = 0;
  g_ctmGUI.frflowCount = 0;
else
  set(hObject, 'Checked', 'on');
  set(handles.menuAutoBeta, 'Checked', 'off');
  g_ctmGUI.betaIndex = 0;
  g_ctmGUI.betaCount = 0;
end

return;

Contact us at files@mathworks.com