function plotMRI( MRI_struct )




% Create labels for MRIs to be used in popup menus:
MRI_list = MRI_struct.MRI_list;
for i=1:length(MRI_list)
    MRI_label{i} = ['MRI ' num2str(i) ': ' num2str(MRI_list(i)) ' year'];
end

% Create labels for frames to be used in popup menus:
length_units = MRI_struct.length_units;
frame_loc = MRI_struct.frame_coords(:,2);
n_f = length(frame_loc);
for i = 1:n_f
    frame_label{i} = ['Frame ' num2str(i) ': y = ' num2str(frame_loc(i)) ' ' length_units];
end

% Create labels for responses to be used in popup menus:
resp_names = MRI_struct.resp_names;
resp_units = MRI_struct.resp_units;
n_r = length(resp_names);
for i = 1:n_r
    resp_label{i} = ['Response ' num2str(i) ': ' resp_names{i} ' (' resp_units{i} ')'];
end

% Create labels for orientations to be used in popup menus:
orientations = MRI_struct.orientations;
n_o = length(orientations);
for i=1:n_o
    orientations_label{i} = [num2str(orientations(i)) ' degrees'];
end

m = 1; % index for MRI to plot
f = 1; % index for frame to plot
r = 1; % index for response to plot
o = 1; % index for orientation to plot

scrsz = get(0,'ScreenSize');
oX = scrsz(3)*.05;
oY = scrsz(4)*.2;
dX = scrsz(3)*.9;
dY = scrsz(4)*.6;
clr = get(0,'DefaultUicontrolBackgroundColor');

side_w = 70; % width of MRI, frame, response selection panel on right side
btm_h = 5; % height of menu panel at bottom


% ************************* Create figure for plotting resampled DIFs:

DIF_fig = figure('Color',clr,'Position',[oX oY dX dY],'Name','Plotting of Resampled DIFs','NumberTitle','off'); 
set( DIF_fig, 'Units', 'character');
Size = get( DIF_fig, 'Position');
X0 = Size(3);
Y0 = Size(4);
plot_h = Y0 - btm_h;
plot_w = X0 - side_w;

toggle_w = X0/3-2; % width of toggle buttons on menu panel
toggle_h = btm_h-1;

DIF_plot_panel = uipanel('Parent',DIF_fig,'BackgroundColor',clr,'Units','character','Position',[0 btm_h plot_w plot_h]);
DIF_axes = axes('Parent',DIF_plot_panel);

DIF_select_panel = uipanel('Parent',DIF_fig,'BackgroundColor',clr,'Units','character','Position',[plot_w btm_h side_w plot_h]);
% 
% 
uicontrol(DIF_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Building Orientation:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-2.5 side_w-4 1.5]);
orientation_popup = uicontrol(DIF_select_panel, 'Style', 'popupmenu', 'String',orientations_label,'Value',o,...
    'Units','characters','Position',[2 plot_h-4 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@OrientationCallback);

uicontrol(DIF_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Frame:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-6.5 side_w-4 1.5]);
DIF_frame_popup = uicontrol(DIF_select_panel, 'Style', 'popupmenu', 'String',frame_label,'Value',f,...
    'Units','characters','Position',[2 plot_h-8 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@DIFFrameSelectCallback);

uicontrol(DIF_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Response:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-10.5 side_w-4 1.5]);
DIF_resp_popup = uicontrol(DIF_select_panel, 'Style', 'popupmenu', 'String',resp_label,'Value',f,...
    'Units','characters','Position',[2 plot_h-12 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@DIFRespSelectCallback);

uicontrol(DIF_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select type of peak:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-14.5 side_w-4 1.5]);
DIF_max_radio = uicontrol(DIF_select_panel, 'Style', 'radiobutton', 'String','Maximum values','Value',1,...
    'Units','characters','Position',[4 plot_h-15.75 side_w-4 1.5],...
    'Callback',@DIFMaxCallback);
DIF_min_radio = uicontrol(DIF_select_panel, 'Style', 'radiobutton', 'String','Minimum values','Value',1,...
    'Units','characters','Position',[4 plot_h-17.25 side_w-4 1.5],...
    'Callback',@DIFMinCallback);

uicontrol(DIF_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select peak evaluation method:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-19.5 side_w-4 1.5]);
DIF_obs_radio = uicontrol(DIF_select_panel, 'Style', 'radiobutton', 'String','Observed peaks (sample max / min)','Value',1,...
    'Enable','inactive','Units','characters','Position',[4 plot_h-20.75 side_w-4 1.5],...
    'Callback',@DIFObsPksCallback);
DIF_est_radio = uicontrol(DIF_select_panel, 'Style', 'radiobutton', 'String','Estimated peaks (from probability distribution)','Value',0,...
    'Units','characters','Position',[4 plot_h-22.25 side_w-4 1.5],...
    'Callback',@DIFEstPksCallback);

DIF_menu_panel = uipanel('Parent',DIF_fig,'BackgroundColor',clr,'Units','character','Position',[0 0 X0 btm_h]);
 
DIF_DIF_toggle = uicontrol(DIF_menu_panel, 'Style','togglebutton','String','Resampled DIFs','Value',1,'Enable','inactive',...
    'BackgroundColor',.87*[1 1 1],'Units','characters','Position',[X0/6-toggle_w/2 0.5 toggle_w toggle_h]);
DIF_MRI_toggle = uicontrol(DIF_menu_panel, 'Style','togglebutton','String','Mean Recurrence Intervals','Value',0,...
    'Units','characters','Position',[X0/2-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@MRIToggleCallback);
DIF_exit_button = uicontrol(DIF_menu_panel, 'Style','pushbutton','String','E x i t',...
    'Units','characters','Position',[X0*5/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@ExitCallback);

% ************************* Create figure for peaks with specified MRI:

MRI_fig = figure('Color',clr,'Position',[oX oY dX dY],'Name','Plotting of Peaks with Specified MRIs','NumberTitle','off'); 
set( MRI_fig, 'Units', 'character');
Size = get( MRI_fig, 'Position');

MRI_plot_panel = uipanel('Parent',MRI_fig,'BackgroundColor',clr,'Units','character','Position',[0 btm_h plot_w plot_h]);
MRI_axes = axes('Parent',MRI_plot_panel);

MRI_select_panel = uipanel('Parent',MRI_fig,'BackgroundColor',clr,'Units','character','Position',[plot_w btm_h side_w plot_h]);


uicontrol(MRI_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Mean Recurrence Interval:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-2.5 side_w-4 1.5]);
MRI_popup = uicontrol(MRI_select_panel, 'Style', 'popupmenu', 'String',MRI_label,'Value',m,...
    'Units','characters','Position',[2 plot_h-4 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@MRISelectCallback);

uicontrol(MRI_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Frame:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-6.5 side_w-4 1.5]);
MRI_frame_popup = uicontrol(MRI_select_panel, 'Style', 'popupmenu', 'String',frame_label,'Value',f,...
    'Units','characters','Position',[2 plot_h-8 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@MRIFrameSelectCallback);

uicontrol(MRI_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select Response:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-10.5 side_w-4 1.5]);
MRI_resp_popup = uicontrol(MRI_select_panel, 'Style', 'popupmenu', 'String',resp_label,'Value',f,...
    'Units','characters','Position',[2 plot_h-12 side_w-4 1.5],'BackgroundColor','w',...
    'Callback',@MRIRespSelectCallback);

uicontrol(MRI_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select type of peak:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-14.5 side_w-4 1.5]);
MRI_max_radio = uicontrol(MRI_select_panel, 'Style', 'radiobutton', 'String','Maximum values','Value',1,...
    'Units','characters','Position',[4 plot_h-15.75 side_w-4 1.5],...
    'Callback',@MRIMaxCallback);
MRI_min_radio = uicontrol(MRI_select_panel, 'Style', 'radiobutton', 'String','Minimum values','Value',1,...
    'Units','characters','Position',[4 plot_h-17.25 side_w-4 1.5],...
    'Callback',@MRIMinCallback);

uicontrol(MRI_select_panel,'BackgroundColor',clr, 'Style', 'text', 'String','Select peak evaluation method:',...
    'HorizontalAlignment','left','Units','characters','Position',[2 plot_h-19.5 side_w-4 1.5]);
MRI_obs_radio = uicontrol(MRI_select_panel, 'Style', 'radiobutton', 'String','Observed peaks (sample max / min)','Value',1,...
    'Enable','inactive','Units','characters','Position',[4 plot_h-20.75 side_w-4 1.5],...
    'Callback',@MRIObsPksCallback);
MRI_est_radio = uicontrol(MRI_select_panel, 'Style', 'radiobutton', 'String','Estimated peaks (from probability distribution)','Value',0,...
    'Units','characters','Position',[4 plot_h-22.25 side_w-4 1.5],...
    'Callback',@MRIEstPksCallback);

MRI_menu_panel = uipanel('Parent',MRI_fig,'BackgroundColor',clr,'Units','character','Position',[0 0 X0 btm_h]);

MRI_DIF_toggle = uicontrol(MRI_menu_panel, 'Style','togglebutton','String','Resampled DIFs','Value',0,...
    'Units','characters','Position',[X0/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@DIFToggleCallback);
MRI_MRI_toggle = uicontrol(MRI_menu_panel, 'Style','togglebutton','String','Mean Recurrence Intervals','Value',1,'Enable','inactive',...
    'BackgroundColor',.87*[1 1 1],'Units','characters','Position',[X0/2-toggle_w/2 0.5 toggle_w toggle_h]);
MRI_exit_button = uicontrol(MRI_menu_panel, 'Style','pushbutton','String','E x i t',...
    'Units','characters','Position',[X0*5/6-toggle_w/2 0.5 toggle_w toggle_h],...
    'Callback',@ExitCallback);

max_obs = MRI_struct.max_obs;
min_obs = MRI_struct.min_obs;
max_est = MRI_struct.max_est;
min_est = MRI_struct.min_est;

if ~((max_obs & min_obs) | (max_est & min_est))
    err = errordlg('No MRI results available for plotting.','Error plotting MRI results');
    uiwait(err); return;
end

if max_obs & min_obs
    MRI_max_obs = MRI_struct.MRI_max_obs;
    MRI_min_obs = MRI_struct.MRI_min_obs;
    MRI_UKO_max_obs = MRI_struct.MRI_UKO_max_obs;
    MRI_UKO_min_obs = MRI_struct.MRI_UKO_min_obs;
else
    set(DIF_obs_radio, 'Value', 0, 'Enable', 'off');
    set(DIF_est_radio, 'Value',1,'Enable', 'inactive');
    set(MRI_obs_radio, 'Value', 0, 'Enable', 'off');
    set(MRI_est_radio, 'Value',1,'Enable', 'inactive');
end    
if max_est & min_est
    MRI_max_est = MRI_struct.MRI_max_est;
    MRI_min_est = MRI_struct.MRI_min_est;
    MRI_UKO_max_est = MRI_struct.MRI_UKO_max_est;
    MRI_UKO_min_est = MRI_struct.MRI_UKO_min_est;
else
    set(DIF_est_radio, 'Value',0, 'Enable', 'off');
    set(MRI_est_radio, 'Value',0, 'Enable', 'off');
end

DIF_struct = MRI_struct.DIF_struct;
DIF_hat = MRI_struct.DIF_hat;

%size([orientations orientations])
%size([squeeze(MRI_max_obs(m,f,:,r)) squeeze(MRI_max_obs(m,f,:,r))])


UpdateDIFplot
UpdateMRIplot;
set(DIF_fig,'Visible','off');

uiwait(MRI_fig);



% -------------------------------------------- Callback functions

function OrientationCallback( src, event )
    o = get( orientation_popup, 'Value');
    UpdateDIFplot;
end

function DIFFrameSelectCallback( src, event )
    f = get( DIF_frame_popup, 'Value');
    set(MRI_frame_popup, 'Value',f);
    UpdateDIFplot;
end

function DIFRespSelectCallback( src, event )
    r = get( DIF_resp_popup, 'Value');
    set(MRI_resp_popup, 'Value',r);
    UpdateDIFplot;
end

function DIFMaxCallback( src, event )
    max_val = get( DIF_max_radio, 'Value');
    if max_val==0
        set( DIF_min_radio, 'Enable', 'inactive');
    else
        set( DIF_min_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
end
function DIFMinCallback( src, event )
    min_val = get( DIF_min_radio, 'Value');
    if min_val==0
        set( DIF_max_radio, 'Enable', 'inactive');
    else
        set( DIF_max_radio, 'Enable', 'on');
    end
    UpdateDIFplot;
end

function DIFObsPksCallback( src, event )
    set( DIF_obs_radio, 'Enable', 'inactive');
    set( DIF_est_radio, 'Value', 0, 'Enable', 'on')
    UpdateDIFplot;
end
function DIFEstPksCallback( src, event )
    set( DIF_est_radio, 'Enable', 'inactive');
    set( DIF_obs_radio, 'Value',0, 'Enable', 'on');
    UpdateDIFplot;
end


function UpdateDIFplot(src, event)
    alpha = orientations(o)+DIF_struct(1).theta;
    alpha(find(alpha<0)) = alpha(find(alpha<0))+360;     % make sure alpha is >=0
    alpha(find(alpha>=360)) = alpha(find(alpha>=360))-360;   % make sure alpha is <360
    alpha_hat = orientations(o) + DIF_hat(o).theta_hat;
    alpha_hat(find(alpha_hat<0)) = alpha_hat(find(alpha_hat<0))+360;     % make sure alpha is >=0
    alpha_hat(find(alpha_hat>=360)) = alpha_hat(find(alpha_hat>=360))-360;   % make sure alpha is <360
    axes(DIF_axes);
    cla reset;
    symbol_list = {'k.','k+'};
    DIF_legend = {};
    if get(DIF_obs_radio, 'Value')
        if get(DIF_max_radio, 'Value') & get(DIF_min_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,[alpha; alpha],[DIF_struct(count).X_max_obs(r,:,f) DIF_struct(count).X_min_obs(r,:,f)],symbol_list{count}); hold on;
                DIF_legend{count} = ['Obs Max/Min - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,[alpha_hat alpha_hat],[DIF_hat(o).X_max_obs(r,:,f) DIF_hat(o).X_min_obs(r,:,f)],'bo'); hold on;
            DIF_legend{count+1} = 'Obs Max/Min - Resampled';
        elseif get(DIF_max_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,alpha,DIF_struct(count).X_max_obs(r,:,f),symbol_list{count}); hold on;
                DIF_legend{count} = ['Obs Max - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,alpha_hat,DIF_hat(o).X_max_obs(r,:,f),'bo'); hold on;
            DIF_legend{count+1} = 'Obs Max - Resampled';
        elseif get(DIF_min_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,alpha,DIF_struct(count).X_min_obs(r,:,f),symbol_list{count}); hold on;
                DIF_legend{count} = ['Obs Min - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,alpha_hat,DIF_hat(o).X_min_obs(r,:,f),'bo'); hold on;
            DIF_legend{count+1} = 'Obs Min - Resampled';
        end
    elseif get(DIF_est_radio, 'Value')
        if get(DIF_max_radio, 'Value') & get(DIF_min_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,[alpha; alpha],[DIF_struct(count).X_max_est(r,:,f) DIF_struct(count).X_min_est(r,:,f)],symbol_list{count}); hold on;
                DIF_legend{count} = ['Est Max/Min - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,[alpha_hat alpha_hat],[DIF_hat(o).X_max_est(r,:,f) DIF_hat(o).X_min_est(r,:,f)],'bo'); hold on;
            DIF_legend{count+1} = 'Est Max/Min - Resampled';
        elseif get(DIF_max_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,alpha,DIF_struct(count).X_max_est(r,:,f),symbol_list{count}); hold on;
                DIF_legend{count} = ['Est Max - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,alpha_hat,DIF_hat(o).X_max_est(r,:,f),'bo'); hold on;
            DIF_legend{count+1} = 'Est Max - Resampled';
        elseif get(DIF_min_radio, 'Value')
            for count = 1:length(DIF_struct);
                plot(DIF_axes,alpha,DIF_struct(count).X_min_est(r,:,f),symbol_list{count}); hold on;
                DIF_legend{count} = ['Est Min - "' strrep(DIF_struct(count).terrain,'_',' ') '" Terrain'];
            end
            plot(DIF_axes,alpha_hat,DIF_hat(o).X_min_est(r,:,f),'bo'); hold on;
            DIF_legend{count+1} = 'Est Min - Resampled';
        end
    end
    v = axis;
    plot([orientations(o) orientations(o)],[v(3) v(4)],'b--'); hold on;
    DIF_legend{count+2} = 'Reference axis of building';
    hold off;
    xlabel('Wind Direction (degrees clockwise from North)');
    ylabel([resp_names{r} ' (' resp_units{r} ')']);
    title(['Peak values of Response ' num2str(r) ' (' resp_names{r} ') for Frame ' ...
        num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']);
    axis([0 360 v(3) v(4)]);
    set(DIF_axes,'XTick',[0:45:360]);
    legend(DIF_legend,'Location','Best');
end





function MRISelectCallback( src, event )
    m = get( MRI_popup, 'Value');
    UpdateMRIplot;
end

function MRIFrameSelectCallback( src, event )
    f = get( MRI_frame_popup, 'Value');
    set(DIF_frame_popup, 'Value',f);
    UpdateMRIplot;
end

function MRIRespSelectCallback( src, event )
    r = get( MRI_resp_popup, 'Value');
    set(DIF_resp_popup, 'Value',r);
    UpdateMRIplot;
end

function MRIMaxCallback( src, event )
    max_val = get( MRI_max_radio, 'Value');
    if max_val==0
        set( MRI_min_radio, 'Enable', 'inactive');
    else
        set( MRI_min_radio, 'Enable', 'on');
    end
    UpdateMRIplot;
end
function MRIMinCallback( src, event )
    min_val = get( MRI_min_radio, 'Value');
    if min_val==0
        set( MRI_max_radio, 'Enable', 'inactive');
    else
        set( MRI_max_radio, 'Enable', 'on');
    end
    UpdateMRIplot;
end

function MRIObsPksCallback( src, event )
    obs_val = get( MRI_obs_radio, 'Value');
    if obs_val==0
        set( MRI_est_radio, 'Enable', 'inactive');
    else
        set( MRI_est_radio, 'Enable', 'on');
    end
    UpdateMRIplot;
end
function MRIEstPksCallback( src, event )
    est_val = get( MRI_est_radio, 'Value');
    if est_val==0
        set( MRI_obs_radio, 'Enable', 'inactive');
    else
        set( MRI_obs_radio, 'Enable', 'on');
    end
    UpdateMRIplot;
end




function UpdateMRIplot(src, event)
    axes(MRI_axes);
    cla reset;
    MRI_legend = {};
    count = 1;
    if get(MRI_obs_radio, 'Value')
        if get(MRI_max_radio, 'Value') & get(MRI_min_radio, 'Value')
            plot(MRI_axes,[orientations orientations],[squeeze(MRI_max_obs(m,f,:,r)); squeeze(MRI_min_obs(m,f,:,r))],'k.'); hold on;
            MRI_legend{count} = 'Obs Max/Min'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_max_obs(m,f,r) MRI_UKO_max_obs(m,f,r)],'k--'); hold on;
            MRI_legend{count} = 'Obs Max (unknown orientation)'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_min_obs(m,f,r) MRI_UKO_min_obs(m,f,r)],'k:'); hold on;
            MRI_legend{count} = 'Obs Min (unknown orientation)'; count = count+1;
        elseif get(MRI_max_radio, 'Value')
            plot(MRI_axes,orientations,squeeze(MRI_max_obs(m,f,:,r)),'k.'); hold on;
            MRI_legend{count} = 'Obs Max'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_max_obs(m,f,r) MRI_UKO_max_obs(m,f,r)],'k--'); hold on;
            MRI_legend{count} = 'Obs Max (unknown orientation)'; count = count+1;
        elseif get(MRI_min_radio, 'Value')
            plot(MRI_axes,orientations,squeeze(MRI_min_obs(m,f,:,r)),'k.'); hold on;
            MRI_legend{count} = 'Obs Min'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_min_obs(m,f,r) MRI_UKO_min_obs(m,f,r)],'k:'); hold on;
            MRI_legend{count} = 'Obs Min (unknown orientation)'; count = count+1;
        end
    end
    if get(MRI_est_radio, 'Value')
        if get(MRI_max_radio, 'Value') & get(MRI_min_radio, 'Value')
            plot(MRI_axes,[orientations orientations],[squeeze(MRI_max_est(m,f,:,r)); squeeze(MRI_min_est(m,f,:,r))],'b.'); hold on;
            MRI_legend{count} = 'Est Max/Min'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_max_est(m,f,r) MRI_UKO_max_est(m,f,r)],'b--'); hold on;
            MRI_legend{count} = 'Est Max (unknown orientation)'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_min_est(m,f,r) MRI_UKO_min_est(m,f,r)],'b:'); hold on;
            MRI_legend{count} = 'Est Min (unknown orientation)'; count = count+1;
        elseif get(MRI_max_radio, 'Value')
            plot(MRI_axes,orientations,squeeze(MRI_max_est(m,f,:,r)),'b.'); hold on;
            MRI_legend{count} = 'Est Max'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_max_est(m,f,r) MRI_UKO_max_est(m,f,r)],'b--'); hold on;
            MRI_legend{count} = 'Est Max (unknown orientation)'; count = count+1;
        elseif get(MRI_min_radio, 'Value')
            plot(MRI_axes,orientations,squeeze(MRI_min_est(m,f,:,r)),'b.'); hold on;
            MRI_legend{count} = 'Est Min'; count = count+1;
            plot(MRI_axes,[0 360],[MRI_UKO_min_est(m,f,r) MRI_UKO_min_est(m,f,r)],'b:'); hold on;
            MRI_legend{count} = 'Est Min (unknown orientation)'; count = count+1;
        end
    end
    hold off;
    xlabel('Building Orientation (degrees clockwise from North)');
    ylabel([resp_names{r} ' (' resp_units{r} ')']);
    title(['Peak values of Response ' num2str(r) ' (' resp_names{r} ') for Frame ' ...
        num2str(f) ' (y = ' num2str(frame_loc(f)) ' ' length_units ')']);
    v = axis;
    axis([0 360 v(3) v(4)]);
    set(MRI_axes,'XTick',[0:45:360]);
    legend(MRI_legend,'Location','Best');
end

function DIFToggleCallback( src, event)
    set( DIF_fig, 'Visible','on','Position',get(MRI_fig,'Position'))
    set( MRI_fig, 'Visible', 'off')
    set( MRI_DIF_toggle, 'Value', 0);
    UpdateDIFplot;
end
function MRIToggleCallback( src, event)
    set( MRI_fig, 'Visible','on','Position',get(DIF_fig,'Position'))
    set( DIF_fig, 'Visible', 'off')
    set( DIF_MRI_toggle, 'Value', 0);
    UpdateMRIplot;
end

function ExitCallback( src, event )
    close(DIF_fig);
    close(MRI_fig);
end


end
