%This program write by Renoald
%Any questions or error in this program please email to Renoald@live.com
function varargout = Leveling(varargin)
% LEVELING M-file for Leveling.fig
%      LEVELING, by itself, creates a new LEVELING or raises the existing
%      singleton*.
%
%      H = LEVELING returns the handle to a new LEVELING or the handle to
%      the existing singleton*.
%
%      LEVELING('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in LEVELING.M with the given input arguments.
%
%      LEVELING('Property','Value',...) creates a new LEVELING or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Leveling_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Leveling_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 Leveling

% Last Modified by GUIDE v2.5 06-Jun-2012 18:22:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Leveling_OpeningFcn, ...
                   'gui_OutputFcn',  @Leveling_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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 Leveling is made visible.
function Leveling_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 Leveling (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

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

function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

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

[FileName,PathName] = uigetfile({'*.dat';'*.txt';'*.*'},'Select the data file');
if FileName~=0
  directory=strcat(PathName ,FileName);
  setappdata(Leveling,'myfile',directory);
   setappdata(Leveling,'file',FileName);
  [ben,V1]=benmark(directory);%ben=bnckmark V=benmark value
[sta1,sta2,V,std]=station(directory);%sta1 and sta1 -stn1 and 2 V=value std=standard deviation
n=length(V1);
fid=fopen('Info','w');
fprintf(fid,'Benchmark\n');
fprintf(fid,'station    Elevation value\n');
num=ones(n,1);
for i=1 : n
    fprintf(fid,'%s           %.4f\n',ben{1,i},V1(i,1));
end
fprintf(fid,'********************************\n');
fprintf(fid,'observations\n');
fprintf(fid,'From   To     Elevation value    (+/-)std\n');
n1=length(V);
num1=ones(n1,1);
num2=ones(n1,1);
for i=1 : n1
    C1=sta1{1,i};
      num1(i,1)=unicode2native(C1);
C2=sta2{1,i};
 num2(i,1)=unicode2native(C2);
end
 for i=1 : n1
    fprintf(fid,'%s      %s       %.5f          %.4f \n',sta1{1,i},sta2{1,i},V(i,1),std(i,1));
 end
  to_tum=[num1
       num2];
 to=unique(to_tum);
 %number of station
 numT=length(to);
 %fprintf('number of station:%d\n',numT);
 %number of parameter

numA=numT-n;
TT=to;
  %fprintf('number of parameter:%d\n',numA);
  for i=1 :  n
      for j=1 : numT
           if num(i,1)==TT(j,1)
              TT(j,1)=0;
           end

          %fprintf('%d : %d \n',i,j)
      end
  end
  K=0;
  para=ones(numA,1);
  for j=1 : numT
  if TT(j,1) ~=0
      K=K+1;
      para(K)=TT(j,1);
  end
  end

  %A matrrik

  A=zeros(n1,numA);
  for i=1 : numA
      for j=1 : n1
           if para(i,1)==num1(j,1)
               A(j,i)=1;

           end
          %fprintf('%d : %d \n',i,j)
      end
  end
  for i=1 : numA
      for j=1 : n1
           if para(i,1)==num2(j,1)
               A(j,i)=-1;
           end
          %fprintf('%d : %d \n',i,j)
      end
  end
 %B matric
B=zeros(n1,1);
for i=1 : n
    for j=1 : n1
        if num(i,1)==num1(j,1)
            B(j,1)=V1(i,1);
        end
    end
end
 for i=1 : n
    for j=1 : n1
        if num(i,1)==num2(j,1)
            B(j,1)=-V1(i,1);
        end
    end
end
%fprintf('%.4f\n',B)
fprintf(fid,'number of benchmark:%d\n',n);
fprintf(fid,'number of station:%d\n',numT);
fprintf(fid,'number of observation:%d\n',n1);
fprintf(fid,'number of parameter:%d\n',numA);
fclose(fid);
fid1=fopen('Info');
indic = 1;
    while 1
         tline = fgetl(fid1);
         if ~ischar(tline),
             break
         end
         strings{indic}=tline;
         indic = indic + 1;
    end
    fclose(fid1);
set(handles.edit1,'String', strings)
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fc=getappdata(Leveling,'myfile');
fN=getappdata(Leveling,'file');
[a1,a2]=size(fN);
%disp(a2)
if a2 > 0
ind=findstr(fN,'.');
%disp(fN(1:ind-1))
output=strcat(fN(1:ind-1) ,'.out') ;
 adj(fc,output)
 fid1=fopen(output);
indic = 1;
    while 1
         tline = fgetl(fid1);
         if ~ischar(tline),
             break
         end
         strings{indic}=tline;
         indic = indic + 1;
    end
    fclose(fid1);
set(handles.edit2,'String', strings)
set(handles.pushbutton3,'Enable','on');
end

function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'String', '')
set(handles.edit2,'String', '')

clear fN
set(handles.pushbutton3,'Enable','off');

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

delete(Leveling)


function id = defaultId(stack)

% DEFAULTID returns a string that can serve as warning or error identifier,
% for example 'FieldTip:ft_read_header:line345'.
%
% See also WARNING, ERROR, FT_NOTICE, FT_INFO, FT_DEBUG

% Copyright (C) 2017, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
%    FieldTrip is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    FieldTrip is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$

if nargin==1
  % stack is already in input, and assumed to be properly pruned
else
  stack = dbstack('-completenames');

  % stack(1).name is defaultId
  % stack(2).name is ft_notification
  % stack(3).name is ft_error, ft_warning, ft_notice, ft_info or ft_debug

  % remove the functions that pertain to the notification system itself
  stack = stack(4:end);

  % remove the non-FieldTrip functions and scripts, these should not be part of the message identifier
  [v, p] = ft_version;
  keep   = startsWith({stack.file}, p);
  stack  = stack(keep);
end

switch numel(stack)
  case 0
    % it is called from the command line
    id = 'FieldTrip:commandline';
  case 1
    id = sprintf('FieldTrip:%s:line%d', stack(1).name, stack(1).line);
  case 2
    id = sprintf('FieldTrip:%s:%s:line%d', stack(2).name, stack(1).name, stack(1).line);
  otherwise
    % it is called from within a function
    name = sprintf('%s:', stack(end:-1:1).name); % this creates something like fun1:fun2:fun3:
    id   = sprintf('FieldTrip:%sline%d', name, stack(1).line);
end

% slashes occur when using nested functions, but are not allowed in the identifier
id(id=='/') = ':';
id(id=='\') = ':';


function plotIVHCallback(command, varargin)
%"plotIVHCallback"
%   Callbacks for IVH plots, all functions are in the expanded view.
%
%JRA 5/20/04
%
% Copyright 2010, Joseph O. Deasy, on behalf of the CERR development team.
%
% This file is part of The Computational Environment for Radiotherapy Research (CERR).
%
% CERR development has been led by:  Aditya Apte, Divya Khullar, James Alaly, and Joseph O. Deasy.
%
% CERR has been financially supported by the US National Institutes of Health under multiple grants.
%
% CERR is distributed under the terms of the Lesser GNU Public License.
%
%     This version of CERR is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
%
% CERR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
% without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with CERR.  If not, see <http://www.gnu.org/licenses/>.
%
%Usage:
%   function plotIVHCallback(command, varargin)

switch upper(command)
    case 'EXPANDEDVIEW'
    %Request to expand the DVH view.
        hFig = get(gcbo, 'parent');
        ud = get(hFig, 'userdata');
        %set(hFig, 'doublebuffer', 'on');

        %Freeze all other figure objects.
        figChild = get(hFig, 'children');
        for i=1:length(figChild)
            try
                set(figChild(i), 'units', 'pixels');
            end
        end

        %Add margin on right side of figure.
        pos = get(hFig, 'position');
        if ~isfield(ud, 'handles')
            pos(3) = pos(3) + 200;
        else
            framePos = get(ud.handles.frame, 'position');
            frameWidth = framePos(3);
            pos(3) = pos(3) + frameWidth;
        end
        set(hFig, 'position', pos);

        %Return units to normalized.
        figChild = get(hFig, 'children');
        for i=1:length(figChild)
            try
                set(figChild(i), 'units', 'normalized');
            end
        end

        %Change menubar item to reflect change back option.
        set(gcbo, 'label', 'Simple Options', 'callback',['plotIVHCallback(''SIMPLEVIEW'')']);

        %Actually perform the initialization.
        if ~isfield(ud, 'handles')
            plotIVHCallback('INITEXPANDED');
            set(hFig, 'toolbar', 'figure');
        else
            try
                set(ud.handles.motionline, 'visible', 'on')
            end

            try
                n = get(ud.handles.strList, 'value');
                if n > 0
                    set(ud.plots(n-1).hLine, 'selected', 'on');
                    hAxis = get(ud.plots(n-1).hLine, 'parent');
                    set(hAxis, 'buttonDownFcn', 'plotIVHCallback(''CLICKINPLOT'')');
                end
            end
        end
        for i=1:length(ud.plots)
            set(ud.plots(i).hLine, 'hittest', 'on', 'buttondownfcn',['plotIVHCallback(''IVHCLICKED'', ' num2str(i) ')']);
        end

    case 'IVHCLICKED'
        clicked = varargin{1};
        hAxis = get(gcbo, 'parent');
        hFig = get(hAxis, 'parent');
        ud = get(hFig, 'userdata');
        set(ud.handles.strList, 'value', clicked+1);
        plotIVHCallback('SELECTIVH');
        return;

    case 'SIMPLEVIEW'
    %Return to the simple view by contracting the margin.
        hFig = get(gcbo, 'parent');
        ud = get(hFig, 'userdata');

        %Freeze all other figure objects.
        figChild = get(hFig, 'children');
        for i=1:length(figChild)
            try
                set(figChild(i), 'units', 'pixels');
            end
        end

        %Remove the margin, using the frame size as guide.
        framePos = get(ud.handles.frame, 'position');
        frameWidth = framePos(3);
        pos = get(hFig, 'position');
        pos(3) = pos(3) - frameWidth;
        set(hFig, 'position', pos);

        try
            set(ud.handles.motionline, 'visible', 'off')
        end

        for i=1:length(ud.plots)
            hAxis = get(ud.plots(i).hLine, 'parent');
            set(ud.plots(i).hLine, 'selected', 'off', 'hittest', 'off');
        end
        set(hAxis, 'buttonDownFcn', '');

        %Return units to normalized.
        figChild = get(hFig, 'children');
        for i=1:length(figChild)
            try
                set(figChild(i), 'units', 'normalized');
            end
        end

        set(gcbo, 'label', 'Expanded Options', 'callback',['plotIVHCallback(''EXPANDEDVIEW'')']);

    case 'INITEXPANDED'
    %If this is the first time the expanded view is requested, draw all the uicontrols.
        barW = 200;
        hFig = get(gcbo, 'parent');

        ud = get(hFig, 'userdata');
        plots = ud.plots;

        hAxis = get(plots(1).hLine, 'parent');

        pos = get(hFig, 'position');
        h = pos(4); w = pos(3);
        units = 'pixels';

        for i=1:length(plots)
            strNames{i} = [plots(i).struct '--' num2str(plots(i).doseNum)];
            set(plots(i).hLine, 'hittest', 'off');
        end
        strNames = {'None' strNames{:}};

        frame.X = w-barW+10;
        frame.Y = 10;
        frame.W = barW-20;
        frame.H = h-20;
        hTmp = uicontrol(hFig, 'style', 'frame', 'units', units, 'position', [frame.X frame.Y frame.W frame.H]);
        frameColor = get(hTmp, 'BackgroundColor');
        delete(hTmp);
        %Dummy frame for units.
        ud.handles.frame = axes('units', units, 'Position', [w-barW 0 barW h], 'visible', 'off');
        axes('units', units, 'Position', [frame.X frame.Y frame.W frame.H], 'color', frameColor, 'ytick',[],'xtick',[], 'box', 'on', 'parent', hFig);

        uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-25 80 15], 'string', 'Selected DVH:', 'horizontalAlignment', 'left');
        ud.handles.strList = uicontrol(hFig, 'style', 'popupmenu', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-25 80 15], 'string', strNames, 'horizontalAlignment', 'left', 'callback', 'plotIVHCallback(''SELECTIVH'');');

        uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-50 80 15], 'string', 'Linestyle:', 'horizontalAlignment', 'left');
        hTmp = axes('units', units, 'Position', [frame.X+90 frame.Y+frame.H-50 80 15], 'color', frameColor, 'ytick',[],'xtick',[], 'box', 'off', 'parent', hFig, 'xcolor', frameColor, 'ycolor', frameColor);
        ud.handles.lineStyle = line([0 1], [.5 .5], 'color', 'red', 'linestyle', '-', 'visible', 'off', 'parent', hTmp);

        ud.handles.doseTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-75 80 15], 'string', 'FromDose:', 'horizontalAlignment', 'left');
        ud.handles.doseVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-75 80 15], 'string', '.', 'horizontalAlignment', 'right');

        ud.handles.statTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-100 80 15], 'string', 'DVH Stats:', 'horizontalAlignment', 'left');

        ud.handles.meanTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+20 frame.Y+frame.H-125 80 15], 'string', 'Mean Scan:', 'horizontalAlignment', 'left');
        ud.handles.meanVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-125 80 15], 'string', '.', 'horizontalAlignment', 'right');

        ud.handles.totalTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+20 frame.Y+frame.H-150 80 15], 'string', 'Total Vol:', 'horizontalAlignment', 'left');
        ud.handles.totalVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-150 80 15], 'string', '.', 'horizontalAlignment', 'right');

        ud.handles.maxTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+20 frame.Y+frame.H-175 80 15], 'string', 'Max Scan:', 'horizontalAlignment', 'left');
        ud.handles.maxVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-175 80 15], 'string', '.', 'horizontalAlignment', 'right');

        ud.handles.minTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+20 frame.Y+frame.H-200 80 15], 'string', 'Min Scan:', 'horizontalAlignment', 'left');
        ud.handles.minVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-200 80 15], 'string', '.', 'horizontalAlignment', 'right');

        ud.handles.abovePtTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-250 170 15], 'string', 'Vol above point in DVH:', 'horizontalAlignment', 'center');
        uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-275 80 15], 'string', 'Scan', 'horizontalAlignment', 'center');
        ud.handles.volTxt = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-275 80 15], 'string', 'Volume', 'horizontalAlignment', 'center');

        ud.handles.scanDoseVal = uicontrol(hFig, 'style', 'edit', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-300 80 20], 'string', '.', 'horizontalAlignment', 'center', 'callback', 'plotIVHCallback(''DOSEVAL'')');
        ud.handles.scanVolVal = uicontrol(hFig, 'style', 'text', 'units', units, 'position', [frame.X+90 frame.Y+frame.H-300 80 15], 'string', '.', 'horizontalAlignment', 'center');

        %control to display legend in new figure
        ud.handles.legendChk = uicontrol(hFig, 'style', 'checkbox', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-350 150 20], 'string', 'Show Legend', 'horizontalAlignment', 'center', 'callback', 'plotIVHCallback(''LEGENDCALL'')');

        %control to export IVH to excel
        ud.handles.exportIVH = uicontrol(hFig, 'style', 'push', 'units', units, 'position', [frame.X+10 frame.Y+frame.H-380 100 20], 'string', 'Export IVH', 'horizontalAlignment', 'center', 'callback', 'plotIVHCallback(''EXPORTIVH'')');

        yLim = get(hAxis, 'yLim');
        ud.handles.motionline = line([0 0], [yLim(1) yLim(2)], 'color', 'blue', 'parent', hAxis, 'hittest', 'off');

        %Return units to normalized.
        figChild = get(hFig, 'children');
        for i=1:length(figChild)
            try
                set(figChild(i), 'units', 'normalized');
            end
        end
        set(hFig, 'userdata', ud);

    case 'SELECTIVH'
        %An IVH has been selected from the list, populate fields.
        if strcmpi(get(gcbo, 'type'), 'line')
            hAxis = get(gcbo, 'parent');
            hFig = get(hAxis, 'parent');
            ud = get(hFig, 'userdata');
            hPopup = ud.handles.strList;
        elseif strcmpi(get(gcbo, 'type'), 'uicontrol')
            hFig = get(gcbo, 'parent');
            ud = get(hFig, 'userdata');
            hPopup = ud.handles.strList;
        end
        val = get(hPopup, 'value');
        val = val - 1;
        hPlots = [ud.plots.hLine];
        set(hPlots, 'selected', 'off');
        if val > 0
            hAxis = get(hPlots(val), 'parent');
            set(hAxis, 'buttonDownFcn', 'plotIVHCallback(''CLICKINPLOT'')');
            set(hPlots(val), 'selected', 'on');
            lS = get(hPlots(val), 'linestyle');
            col = get(hPlots(val), 'color');
            lW = get(hPlots(val), 'linewidth');
            set(ud.handles.lineStyle, 'linestyle', lS, 'color', col, 'linewidth', lW, 'visible', 'on');

            dose = str2double(get(ud.handles.scanDoseVal, 'string'));
            currentPlot = get(ud.handles.strList, 'value') - 1;
            xLim = get(hAxis, 'xLim');
            if dose < xLim(1)
                dose = xLim(1);
            elseif dose > xLim(2)
                dose = xLim(2);
            end
            surfVol = interp1(ud.plots(currentPlot).xVals, ud.plots(currentPlot).yVals, dose);
            if isnan(surfVol)
                surfVol = 0;
            end
            if ~strcmpi(ud.plots(currentPlot).abs, 'ABS')
                surfVol = surfVol*100;
            end
            set(ud.handles.motionline, 'xdata', [dose dose], 'visible', 'on');
            set(ud.handles.scanDoseVal, 'string', num2str(dose));
            set(ud.handles.scanVolVal, 'string', num2str(surfVol));
            %If we are dealing with a DSH, rename fields and use different
            %metrics calculations from DVH.
            if strcmpi(ud.plots(val).type, 'DSH')
                areaV = ud.plots(val).volsV;
                dosesV = ud.plots(val).doseBins;
                meanD = sum(dosesV.*areaV)/sum(areaV);
                totalArea = sum(areaV);
                maxDose = max(dosesV);
                minDose = min(dosesV);
                set(ud.handles.statTxt, 'string', 'DSH Stats:')
                set(ud.handles.totalTxt, 'string', 'Total Surface:')
                set(ud.handles.meanVal, 'string', num2str(meanD))
                set(ud.handles.totalVal, 'string', num2str(totalArea))
                set(ud.handles.maxVal, 'string', num2str(maxDose));
                set(ud.handles.minVal, 'string', num2str(minDose));
                set(ud.handles.doseVal, 'string', ud.plots(val).doseName);
                if strcmpi(ud.plots(val).abs, 'ABS')
                    set(ud.handles.abovePtTxt, 'string', 'Abs Surface above selected level:')
                    set(ud.handles.volTxt, 'string', 'Abs Surface');
                else
                    set(ud.handles.abovePtTxt, 'string', '% Surface above selected level:')
                    set(ud.handles.volTxt, 'string', '% Surface');
                end

            %If we are dealing with a IVH, rename fields and use different
            %metrics calculations from ISH.
            elseif strcmpi(ud.plots(val).type, 'IVH')
                volHistV = ud.plots(val).volsV;
                doseBinsV = ud.plots(val).doseBins;
                meanD = sum(doseBinsV.*volHistV)/sum(volHistV);
                totalVol = sum(volHistV);
                ind = max(find([volHistV~=0]));
                maxD = doseBinsV(ind);
                ind = min(find([volHistV~=0]));
                minD = doseBinsV(ind);
                set(ud.handles.statTxt, 'string', 'IVH Stats:')
                set(ud.handles.totalTxt, 'string', 'Total Volume:')
                set(ud.handles.meanVal, 'string', num2str(meanD))
                set(ud.handles.totalVal, 'string', num2str(totalVol))
                set(ud.handles.maxVal, 'string', num2str(maxD));
                set(ud.handles.minVal, 'string', num2str(minD));
                set(ud.handles.doseVal, 'string', ud.plots(val).doseName);
                if strcmpi(ud.plots(val).abs, 'ABS')
                    set(ud.handles.abovePtTxt, 'string', 'Abs Volume above selected level:')
                    set(ud.handles.volTxt, 'string', 'Abs Volume');
                else
                    set(ud.handles.abovePtTxt, 'string', '% Volume above selected level:')
                    set(ud.handles.volTxt, 'string', '% Volume');
                end
            else
                error('Not a valid plot type for IVH callback.')
            end
        else
            hAxis = get(hPlots(1), 'parent');
            set(hAxis, 'buttonDownFcn', '')
            set(ud.handles.lineStyle, 'visible', 'off');
            set(ud.handles.motionline, 'visible', 'off');
        end
        set(hFig, 'userdata', ud);

    case 'CLICKINPLOT'
    %A DVH is selected and the plot has been clicked, update guide line/fields.
        hAxis = gcbo;
        hFig = get(hAxis, 'parent');
        ud = get(hFig, 'userdata');
        set(hFig, 'WindowButtonUpFcn', 'plotIVHCallback(''UNCLICKINPLOT'')');
        set(hFig, 'WindowButtonMotionFcn', 'plotIVHCallback(''PLOTMOTION'')');
        currentPlot = get(ud.handles.strList, 'value') - 1;
        cP = get(hAxis, 'currentpoint');
        %Interpolate Surf/Vol, display.
        dose = cP(1,1);
        xLim = get(hAxis, 'xLim');
        if dose < xLim(1)
            dose = xLim(1);
        elseif dose > xLim(2)
            dose = xLim(2);
        end
        surfVol = interp1(ud.plots(currentPlot).xVals, ud.plots(currentPlot).yVals, dose);
        if isnan(surfVol)
            surfVol = 0;
        end
        if ~strcmpi(ud.plots(currentPlot).abs, 'ABS')
            surfVol = surfVol*100;
        end
        set(ud.handles.motionline, 'xdata', [dose dose], 'visible', 'on');
        set(ud.handles.scanDoseVal, 'string', num2str(dose));
        set(ud.handles.scanVolVal, 'string', num2str(surfVol));

    case 'UNCLICKINPLOT'
        hFig = gcbo;
        ud = get(hFig, 'userdata');
        set(hFig, 'WindowButtonMotionFcn', '');
        set(hFig, 'WindowButtonUpFcn', '');
        set(hFig, 'userdata', ud);

    case 'DOSEVAL'
    %A value has been entered into the doseVal field.
        hFig = get(gcbo, 'parent');
        dose = str2double(get(gcbo, 'string'));
        ud = get(hFig, 'userdata');
        currentPlot = get(ud.handles.strList, 'value') - 1;
        hAxis = get(ud.plots(currentPlot).hLine, 'parent');
        xLim = get(hAxis, 'xLim');
        if dose < xLim(1)
            dose = xLim(1);
        elseif dose > xLim(2)
            dose = xLim(2);
        end
        surfVol = interp1(ud.plots(currentPlot).xVals, ud.plots(currentPlot).yVals, dose);
        if isnan(surfVol)
            surfVol = 0;
        end
        if ~strcmpi(ud.plots(currentPlot).abs, 'ABS')
            surfVol = surfVol*100;
        end
        set(ud.handles.motionline, 'xdata', [dose dose], 'visible', 'on');
        set(ud.handles.scanDoseVal, 'string', num2str(dose));
        set(ud.handles.scanVolVal, 'string', num2str(surfVol));

    case 'PLOTMOTION'
    %An IVH is selected, the plot has been clicked and motion is occuring. Update guide line/fields.
        hFig = gcbo;
        ud = get(hFig, 'userdata');
        currentPlot = get(ud.handles.strList, 'value') - 1;
        hAxis = get(ud.plots(currentPlot).hLine, 'parent');
        cP = get(hAxis, 'currentpoint');
        dose = cP(1,1);
        xLim = get(hAxis, 'xLim');
        if dose < xLim(1)
            dose = xLim(1);
        elseif dose > xLim(2)
            dose = xLim(2);
        end
        surfVol = interp1(ud.plots(currentPlot).xVals, ud.plots(currentPlot).yVals, dose);
        if isnan(surfVol)
            surfVol = 0;
        end
        if ~strcmpi(ud.plots(currentPlot).abs, 'ABS')
            surfVol = surfVol*100;
        end
        set(ud.handles.motionline, 'xdata', [dose dose], 'visible', 'on');
        set(ud.handles.scanDoseVal, 'string', num2str(dose));
        set(ud.handles.scanVolVal, 'string', num2str(surfVol));

    case 'LEGENDCALL'
        %executes when "Show Legend" is toggled on or off

        value = get(gcbo,'value');
        hLegend = findobj('tag','IVH_Legend');
        close(hLegend)
        if value == 0
            return;
        end
        hFig = get(gcbo, 'parent');
        ud = get(hFig, 'userdata');
        hLegend = figure('tag','IVH_Legend','name','IVH Legend','numberTitle','off','menubar','Figure','color','w');
        position = get(hLegend,'position');
        figColor = get(hLegend,'color');
        axisLegend = axes('units', 'normalized', 'Position', [0 0 1 1], 'color', figColor, 'ytick',[],'xtick',[], 'box', 'off', 'parent', hLegend,'nextPlot','add','units','normalized','visible','off');
        numLines = length(ud.plots);
        dy = 0.8/numLines;
        position(3) = 400;
        position(4) = (numLines+1)*30;
        set(hLegend,'position',position)

        for i = 1:numLines
            LineStyle = get(ud.plots(i).hLine,'LineStyle');
            LineWidth = get(ud.plots(i).hLine,'LineWidth');
            Color = get(ud.plots(i).hLine,'Color');
            line([0.05 0.15],[0.8-(i-1)*dy 0.8-(i-1)*dy],'LineStyle',LineStyle,'LineWidth',LineWidth,'Color',Color,'parent',axisLegend)
            txt = ['Struct = ',ud.plots(i).struct,', Scan = ',ud.plots(i).doseName];
            text(0.18,0.8-(i-1)*dy,txt)
        end
        axis(axisLegend,[0 1 0 1])

    case 'EXPORTIVH'
        hFig = get(gcbo, 'parent');
        ud = get(hFig, 'userdata');
        currentIVH = get(ud.handles.strList, 'value') - 1;
        if currentIVH == 0
            warndlg('Please select IVH to be exported.','IVH not selected','modal')
            return;
        end

        M = [ud.plots(currentIVH).xVals; ud.plots(currentIVH).yVals];
        %Export only NumPts points
        NumPts = 65000;
        if size(M,2)>NumPts
            indAll = round(linspace(1,size(M,2),NumPts));
            M = M(:,indAll);
        end
        [fname, pname] = uiputfile('*.xls','Save the IVH data as:');
        if isnumeric(fname)
            return;
        end
        try
            xlswrite(fullfile(pname,fname), M');
        catch
            csvwrite(fullfile(pname,fname), M');
        end

end