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

rdGUI(varargin)
function varargout = rdGUI(varargin)
% RDGUI M-file for rdGUI.fig
%      RDGUI, by itself, creates a new RDGUI or raises the existing
%      singleton*.
%
%      H = RDGUI returns the handle to a new RDGUI or the handle to
%      the existing singleton*.
%
%      RDGUI('Property','Value',...) creates a new RDGUI using the
%      given property value pairs. Unrecognized properties are passed via
%      varargin to rdGUI_OpeningFcn.  This calling syntax produces a
%      warning when there is an existing singleton*.
%
%      RDGUI('CALLBACK') and RDGUI('CALLBACK',hObject,...) call the
%      local function named CALLBACK in RDGUI.M with the given input
%      arguments.
%
%      *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 rdGUI

% Last Modified by GUIDE v2.5 26-Jul-2006 16:31:12

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rdGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @rdGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
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 rdGUI is made visible.
function rdGUI_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% First, we shall copy all necessary data from the global 'g_ctmGUI' structure

global g_ctmGUI;  % MUST (!) exist - the call to 'rdGUI' is initiated from 'ctmGUI'
global g_rdGUI;

g_rdGUI.configFile = g_ctmGUI.configFile;
g_rdGUI.cellData   = g_ctmGUI.cellData;
g_rdGUI.pmData     = g_ctmGUI.pmData;
g_rdGUI.freeway    = g_ctmGUI.freeway;
g_rdGUI.inflow     = g_ctmGUI.inflow;
g_rdGUI.outflow    = g_ctmGUI.outflow;

g_rdGUI.lastOR     = 1;
g_rdGUI.lastFR     = 1;

xmin = g_rdGUI.cellData(1).PMstart;
xmax = g_rdGUI.cellData(end).PMend;
if xmin <= xmax
  g_rdGUI.xLims = [xmin xmax];
else
  g_rdGUI.xLims = [xmax xmin];
end

g_rdGUI.OK = 0;


set(handles.chOR, 'Value', 1);
set(handles.chFR, 'Value', 0);
set(handles.header, 'String', [g_rdGUI.freeway ' on-ramp data']);
set(handles.valueTag, 'String', 'Flow (vph) = ');


[g_rdGUI.onRamps, g_rdGUI.offRamps] = get_ramp_lists(g_rdGUI.cellData);
set(handles.rampList, 'String', g_rdGUI.onRamps);
set(handles.valueText, 'String', num2str(g_rdGUI.inflow));


axes(handles.direction)
if g_rdGUI.pmData(1) <= g_rdGUI.pmData(end)
  image(imread('lr.jpg'));
else
  image(imread('rl.jpg'));
end
axis off;

axes(handles.barchart);
plotbar(g_rdGUI.pmData, get_or_flows(g_rdGUI.cellData), 'b');
lims = axis;
axis([g_rdGUI.xLims lims(3:4)]);
xlabel('Post mile');
ylabel('On-ramp flows (vph)');

return;





% --- Outputs from this function are returned to the command line.
function varargout = rdGUI_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;





% --- Executes during object creation, after setting all properties.
function rampList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rampList (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 rampList.
function rampList_Callback(hObject, eventdata, handles)
% hObject    handle to rampList (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 rampList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from rampList

global g_rdGUI;


if get(handles.chOR, 'Value') > 0  % on-ramps are selected
  g_rdGUI.lastOR = get(hObject, 'Value');
  indx           = g_rdGUI.lastOR - 1;
  axes(handles.barchart);
  plotbar(g_rdGUI.pmData, get_or_flows(g_rdGUI.cellData), 'b');
  lims = axis;
  axis([g_rdGUI.xLims lims(3:4)]);
  xlabel('Post mile');
  ylabel('On-ramp flows (vph)');
  hold on;
  if indx == 0
    val = g_rdGUI.inflow;
    set(handles.valueText, 'String', num2str(val));
  elseif indx > size(g_rdGUI.cellData, 2)
    val = g_rdGUI.outflow;
    set(handles.valueText, 'String', num2str(val));
  else
    val = g_rdGUI.cellData(indx).ORflow;
    plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');
    if isempty(g_rdGUI.cellData(indx).ORname)
      set(handles.valueText, 'String', '');
      set(handles.valueText, 'Enable', 'off');
    else
      set(handles.valueText, 'Enable', 'on');
      set(handles.valueText, 'String', num2str(val));
    end
  end
else  % off-ramps are selected
  indx = get(hObject, 'Value');
  g_rdGUI.lastFR = indx;
  val            = g_rdGUI.cellData(indx).FRbeta;
  if isempty(g_rdGUI.cellData(indx).FRname)
    set(handles.valueText, 'String', '');
    set(handles.valueText, 'Enable', 'off');
  else
    set(handles.valueText, 'Enable', 'on');
    set(handles.valueText, 'String', num2str(val));
  end
  axes(handles.barchart);
  plotbar(g_rdGUI.pmData, get_fr_splitratios(g_rdGUI.cellData), 'c');
  lims = axis;
  axis([g_rdGUI.xLims lims(3:4)]);
  xlabel('Post mile');
  ylabel('Off-ramp split ratios');
  hold on;
  plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');
end

hold off;

return;





% --- Executes during object creation, after setting all properties.
function valueText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to valueText (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 valueText_Callback(hObject, eventdata, handles)
% hObject    handle to valueText (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 valueText as text
%        str2double(get(hObject,'String')) returns contents of valueText as a double

global g_rdGUI;


if get(handles.chOR, 'Value') > 0  % on-ramps are selected
  indx = get(handles.rampList, 'Value') - 1;
  val  = str2double(get(hObject, 'String'));
  if ~(val > 0)
    val = 0;
  end
  if indx == 0
    g_rdGUI.inflow = val;  % set new value for in-flow
  elseif indx > size(g_rdGUI.cellData, 2)
    g_rdGUI.outflow = val;  % set new value for out-flow
  else
    if val > g_rdGUI.cellData(indx).ORfmax
      val = g_rdGUI.cellData(indx).ORfmax;
    end
    if ~isempty(g_rdGUI.cellData(indx).ORname)
      g_rdGUI.cellData(indx).ORflow = val;  % set new value for current on-ramp flow
      axes(handles.barchart);
      plotbar(g_rdGUI.pmData, get_or_flows(g_rdGUI.cellData), 'b');
      lims = axis;
      axis([g_rdGUI.xLims lims(3:4)]);
      xlabel('Post mile');
      ylabel('On-ramp flows (vph)');
      hold on;
      plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');
    else
      set(hObject, 'String', '');
    end
  end
else  % off-ramps are selected
  indx = get(handles.rampList, '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_rdGUI.cellData(indx).FRname)
    g_rdGUI.cellData(indx).FRbeta = val;  % set new value for current off-ramp split ratio
    axes(handles.barchart);
    plotbar(g_rdGUI.pmData, get_fr_splitratios(g_rdGUI.cellData), 'c');
    lims = axis;
    axis([g_rdGUI.xLims lims(3:4)]);
    xlabel('Post mile');
    ylabel('Off-ramp split ratios');
    hold on;
    plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');
  else
    set(hObject, 'String', '');
  end
end

hold off;

return;




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

global g_rdGUI;

save_rd(g_rdGUI.cellData, g_rdGUI.inflow, g_rdGUI.outflow, g_rdGUI.configFile);

return;





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

rdGUIclose;

return;


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

global g_rdGUI;

g_rdGUI.OK = 1;

rdGUIclose;

return;





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

% Hint: get(hObject,'Value') returns toggle state of chOR

global g_rdGUI;

set(hObject, 'Value', 1);
set(handles.chFR, 'Value', 0);

set(handles.header, 'String', [g_rdGUI.freeway ' on-ramp data']);
set(handles.valueTag, 'String', 'Flow (vph) = ');
set(handles.rampList, 'String', g_rdGUI.onRamps);
set(handles.rampList, 'Value', g_rdGUI.lastOR);

axes(handles.barchart);
plotbar(g_rdGUI.pmData, get_or_flows(g_rdGUI.cellData), 'b');
lims = axis;
axis([g_rdGUI.xLims lims(3:4)]);
xlabel('Post mile');
ylabel('On-ramp flows (vph)');

hold on;

indx = get(handles.rampList, 'Value') - 1;
if indx == 0
  val = g_rdGUI.inflow;
  set(handles.valueText, 'String', num2str(val));
elseif indx > size(g_rdGUI.cellData, 2)
  val = g_rdGUI.outflow;
  set(handles.valueText, 'String', num2str(val));
else
  val = g_rdGUI.cellData(indx).ORflow;
  plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');
  if isempty(g_rdGUI.cellData(indx).ORname)
    set(handles.valueText, 'String', '');
    set(handles.valueText, 'Enable', 'off');
  else
    set(handles.valueText, 'Enable', 'on');
    set(handles.valueText, 'String', num2str(val));
  end
end

hold off;

return;





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

% Hint: get(hObject,'Value') returns toggle state of chFR

global g_rdGUI;

set(hObject, 'Value', 1);
set(handles.chOR, 'Value', 0);

set(handles.header, 'String', [g_rdGUI.freeway ' off-ramp data']);
set(handles.valueTag, 'String', 'Split Ratio = ');
set(handles.rampList, 'String', g_rdGUI.offRamps);
set(handles.rampList, 'Value', g_rdGUI.lastFR);

axes(handles.barchart);
plotbar(g_rdGUI.pmData, get_fr_splitratios(g_rdGUI.cellData), 'c');
lims = axis;
axis([g_rdGUI.xLims lims(3:4)]);
xlabel('Post mile');
ylabel('Off-ramp split ratios');

hold on;

indx = get(handles.rampList, 'Value');
val  = g_rdGUI.cellData(indx).FRbeta;

if isempty(g_rdGUI.cellData(indx).FRname)
  set(handles.valueText, 'String', '');
  set(handles.valueText, 'Enable', 'off');
else
  set(handles.valueText, 'Enable', 'on');
  set(handles.valueText, 'String', num2str(val));
end

plotbar([g_rdGUI.cellData(indx).PMstart g_rdGUI.cellData(indx).PMend], val, 'r');

hold off;

return;

Contact us at files@mathworks.com