Code covered by the BSD License  

Highlights from
TNMR GUI

image thumbnail
from TNMR GUI by Brandon Armstrong
A GUI for visualizing and analyzing TNMR 1-D data sets.

NMR_UI(varargin)
function varargout = NMR_UI(varargin)
% NMR_UI MATLAB code for NMR_UI.fig
%      
%      This code works with TNMR data 
%
%      NMR_UI, by itself, creates a new NMR_UI or raises the existing
%      singleton*.
%
%      H = NMR_UI returns the handle to a new NMR_UI or the handle to
%      the existing singleton*.
%
%      NMR_UI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NMR_UI.M with the given input arguments.
%
%      NMR_UI('Property','Value',...) creates a new NMR_UI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before NMR_UI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to NMR_UI_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 NMR_UI

% Last Modified by GUIDE v2.5 04-Jun-2013 22:05:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @NMR_UI_OpeningFcn, ...
                   'gui_OutputFcn',  @NMR_UI_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 NMR_UI is made visible.
function NMR_UI_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 NMR_UI (see VARARGIN)

% Upon startig NMR_UI, you will be asked to open a .tnt file by
% Read_Tecmag. This data should a single, or series of FIDs

[handles.data, header, sw] = Read_Tecmag;
handles.current_data = handles.data(:,1);
[pnts, numfids] = size(handles.data);

%Find the number of FIDS to initialize the maximum value of the
%data_selector.

if numfids > 1
    set(handles.data_selector, 'Min',1);
    set(handles.data_selector, 'Max', numfids);
    set(handles.data_selector, 'Value',1);
    set(handles.data_selector, 'SliderStep', [1/numfids, 1/numfids]);
else %an error occurs if Min = Max, so change it slightly.
    set(handles.data_selector, 'Min', 1)
    set(handles.data_selector, 'Max', 1.2)
    set(handles.data_selector, 'Value', 1)
    set(handles.data_selector, 'SliderStep',[1,1]);
end
dwell = header.dwell(1);
acqtime = header.acq_time;
% Make the time axis for the FIDs
handles.fidtime = linspace(dwell, acqtime, pnts);

%Initialize all the processed results.
handles.pro = zeros(pnts, numfids);
handles.integrals = zeros(1,numfids);
handles.x4columns = zeros(1,numfids);
handles.peaks = zeros(1,numfids);
handles.snr = zeros(1,numfids);
handles.phase = zeros(1,numfids);
handles.linebroad = zeros(1,numfids);

plot(handles.fidtime,real(handles.current_data))

% Set the axis for FT data.
bw = 2*sw(1);
handles.hz = linspace(-bw/2, bw/2, pnts);

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

% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = NMR_UI_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 on button press in load_data.
function load_data_Callback(hObject, eventdata, handles)
% hObject    handle to load_data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% See the Opening function as most is repeated here. The main difference is
% many of the sliders and text boxes must be reinitialized to zero when
% loading new data.

[handles.data, header, sw] = Read_Tecmag;
handles.current_data = handles.data(:,1);
[pnts, numfids] = size(handles.data);
set(handles.data2display, 'Value', 1);
set(handles.snrleft, 'String', 0);
set(handles.snrright, 'String', 0);
set(handles.phaseangle, 'String', 0);
set(handles.phase_slider, 'Value', 0);
set(handles.baseline_left1, 'String', 0);
set(handles.baseline_left2, 'String', 0);
set(handles.baseline_right1, 'String', 0);
set(handles.baseline_right2, 'String', 0);
set(handles.snr_display, 'String', 0);
set(handles.datanum_display, 'String', 1);
set(handles.intleft, 'String', 0);
set(handles.intright, 'String', 0);

if numfids > 1
    set(handles.data_selector, 'Min',1);
    set(handles.data_selector, 'Max', numfids);
    set(handles.data_selector, 'Value',1);
    set(handles.data_selector, 'SliderStep', [1/numfids, 1/numfids]);
else
    set(handles.data_selector, 'Min', 1)
    set(handles.data_selector, 'Max', 1.2)
    set(handles.data_selector, 'Value', 1)
    set(handles.data_selector, 'SliderStep',[1,1]);
end
dwell = header.dwell(1);
acqtime = header.acq_time;
handles.fidtime = linspace(dwell, acqtime, pnts);

handles.pro = zeros(pnts, numfids);
handles.integrals = zeros(1,numfids);
handles.x4columns = zeros(1,numfids);
handles.peaks = zeros(1,numfids);
handles.snr = zeros(1,numfids);
handles.phase = zeros(1,numfids);
handles.linebroad = zeros(1,numfids);

plot(handles.fidtime,real(handles.current_data))

bw = 2*sw(1);
handles.hz = linspace(-bw/2, bw/2, pnts);

guidata(hObject, handles)


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

% Choose what data to display.

val = get(hObject,'Value');
str = get(hObject,'String');
datanum = round(get(handles.data_selector, 'Value'));
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
switch str{val}
    case 'FID'
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.fidtime,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
    case 'FT'
        lb = str2double(get(handles.lb, 'String'));
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Processed'
        handles.current_data = handles.pro(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Integrals'
        handles.current_data = handles.integrals;
        if ~any(handles.x4columns)
            plot(handles.current_data, 'ko')
        else
            plot(handles.x4columns, handles.current_data, 'ko')
        end
        
     case 'Peaks'
        handles.current_data = handles.peaks;
        if ~any(handles.x4columns)
            plot(handles.current_data, 'ko')
        else
            plot(handles.x4columns, handles.current_data, 'ko')
        end
        
     case 'SNR'
        handles.current_data = handles.snr;
        if ~any(handles.x4columns)
            plot(handles.current_data, 'ko')
        else
            plot(handles.x4columns, handles.current_data, 'ko')
        end
        
      case 'Applied Phase'
        handles.current_data = handles.phase;
        if ~any(handles.x4columns)
            plot(handles.current_data, 'ko')
        else
            plot(handles.x4columns, handles.current_data, 'ko')
        end

end

guidata(hObject, handles);


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

% This function changes adjust the phase of the spectrum if you are looking
% an FT. If looking at 'Processed' FT, it switches the data2display to FT
% and does not save anything. 

phi = pi*get(hObject, 'Value')/180;
set(handles.phaseangle,'String',num2str(phi*180/pi));
lb = str2double(get(handles.lb, 'String'));
datanum = round(get(handles.data_selector, 'Value'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');

switch str{val}
    case 'FID'
        %If displaying FID, keep displaying FID, don't do anything.
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.fidtime,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
    case 'FT'

        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end

    case 'Processed'
        %If processed, assume user wants to phase data, so set to FT and update plot
        %but do NOT overwrite processed data.
        
        set(handles.data2display,'Value', 2);
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    otherwise
        %do nothing
            
end
guidata(hObject,handles)


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

% This function is the call back to the text box displaying the current phase.
% It does not change any saved data. If you are in 'Processed',
% it changes data2display to FT and updates the plot, but does not
% overwrite any processed data. If you are looking at integrals, peaks, or
% snr, the changing the phase, updates the phase values, but they will not
% be applied until switching to look at FT data.

angle = str2double(get(hObject, 'String'));
set(handles.phase_slider, 'Value', angle);
phi = pi*angle/180;
lb = str2double(get(handles.lb, 'String'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
datanum = round(get(handles.data_selector, 'Value'));

switch str{val}
    case 'FID'
        %If displaying FID, keep displaying FID, don't do anything.
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
        
    case 'FT'

        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        

    case 'Processed'
        %If processed, assume user wants to phase data, so FT and update plot
        %but do NOT overwrite processed data.
        set(handles.data2display,'Value', 2);
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
        
    otherwise
        %do nothing, but leave values updated for future.

end

guidata(hObject, handles);

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

lb = str2double(get(hObject,'String'));
datanum = round(get(handles.data_selector, 'Value'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');


switch str{val}
    case 'FID'
        % Do nothing
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.fidtime,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
    case 'FT'
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Processed'
        set(handles.data2display, 'Value', 2)
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    otherwise
        
        %do nothing
end

guidata(hObject, handles);

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

% This function puts the current diplayed data set into handles.pro as
% processed data with the current phase and lb applied. If FID is diplayed,
% it executes the FT with applied settings and saves it into handles.pro
% and updates data2display to processed. If looking at the FT, it remains
% in FT, but does save the data. This is done so you can continue flipping
% through FT without having to update the Data to Display pull down menu.
% If looking at 'Processed', it will apply with the current settings (so it
% overwrites and updates the display.
% If integrals, peaks, or snr is displayed it does nothing.

% !!!!! An important note about Apply 1 and Apply All: If previously a
% baseline correction had been performed, it is no longer saved! Baseline
% correction will have to be redone if desired.

lb = str2double(get(handles.lb, 'String'));
phi = pi*get(handles.phase_slider, 'Value')/180;
datanum = round(get(handles.data_selector, 'Value'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');


switch str{val}
    case 'FID'
        %If FID, go ahead and apply, but switch to 'Processed' so we know
        %what happened.
        
        handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        handles.phase(datanum) = phi*180/pi;
        handles.current_data = handles.pro(:,datanum);
        handles.linebroad(datanum) = lb;
        set(handles.data2display, 'Value', 3);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'FT'
        %Apply, but leave 'Data to Display' alone so we can quickly go to
        %the next spectrum.
        handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        handles.phase(datanum) = phi*180/pi;
        handles.linebroad(datanum) = lb;
        handles.current_data = handles.pro(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Processed'
        handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        handles.phase(datanum) = phi*180/pi;
        handles.linebroad(datanum) = lb;
        handles.current_data = handles.pro(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    otherwise
        beep
        errordlg('Not doing anything: Choose data before using apply')
end
            
guidata(hObject, handles);

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

% See apply 1 notes.

lb = str2double(get(handles.lb, 'String'));
phi = pi*get(handles.phase_slider, 'Value')/180;
numfids = get(handles.data_selector, 'Max');
val = get(handles.data2display,'Value');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
datanum = round(get(handles.data_selector, 'Value'));

if val <= 3 %If FID, FT, or Processed, apply all and update plot and data2display
        
        for m = 1:numfids
         handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
         handles.phase(m) = phi*180/pi;
         handles.linebroad(m) = lb;
        end
        
        handles.current_data = handles.pro(:,datanum);
        set(handles.data2display, 'Value', 3);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
else
         beep
         errordlg('Not doing anything: Choose data before using apply')
end

guidata(hObject, handles);

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

datarange = inputdlg({'Enter the 1st index to use', 'Enter the 2nd index to use'});

index1 = round(str2double(datarange{1}));
index2 = round(str2double(datarange{2}));
lb = str2double(get(handles.lb, 'String'));
phi = pi*get(handles.phase_slider, 'Value')/180;
numfids = get(handles.data_selector, 'Max');
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
datanum = round(get(handles.data_selector, 'Value'));


if index1 < 1 || index1 > numfids || index2 > numfids
    
    beep
    errordlg('Invalid Range: Data does not exist')
    
elseif index2 < index1
    
    beep
    errordlg('Invalid Range: Left entry must be less than right')
    
else
    
  if val <= 3
        
        for m = index1:index2
         handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
         handles.phase(m) = phi*180/pi;
         handles.linebroad(m) = lb;
        end
        
        handles.current_data = handles.pro(:,datanum);
        set(handles.data2display, 'Value', 3);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
        
  else
        
        beep
        errordlg('Not doing anything: Choose data before using apply')
  end
end
guidata(hObject, handles);



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

% This function updates the plot the the newly selected data#. If a spectrum 
% is not being viewed, it does nothing.

datanum = round(get(hObject, 'Value'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
set(handles.datanum_display,'String',num2str(datanum));


switch str{val}
    case 'FID'
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.fidtime,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
    case 'FT'
        phi = pi*get(handles.phase_slider, 'Value')/180;
        lb = str2double(get(handles.lb, 'String'));
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Processed'
        handles.current_data = handles.pro(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    otherwise
        
        %do nothing
end

guidata(hObject, handles);


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

% Same as data selector, but the user has typed in a data#

datanum = round(str2double(get(hObject, 'String')));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
numfids = length(handles.data(1,:));

if datanum < 1 || datanum > numfids
    
    beep
    errordlg('Hey, that data does not exist')
    
else
    
set(handles.data_selector, 'Value', datanum);
    
switch str{val}
    case 'FID'
        handles.current_data = handles.data(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.fidtime,real(handles.current_data))
            case 'imagData'
                plot(handles.fidtime,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.fidtime,real(handles.current_data),'b',...
                    handles.fidtime,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.fidtime,abs(handles.current_data));
        end
        
    case 'FT'
        phi = pi*get(handles.phase_slider, 'Value')/180;
        lb = str2double(get(handles.lb, 'String'));
        handles.current_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    case 'Processed'
        handles.current_data = handles.pro(:,datanum);
        switch dataTag
            case 'realData'
                plot(handles.hz,real(handles.current_data))
            case 'imagData'
                plot(handles.hz,imag(handles.current_data),'r')
            case 'ReImdata'
                plot(handles.hz,real(handles.current_data),'b',...
                    handles.hz,imag(handles.current_data),'r');
            case 'magnitudeData'
                plot(handles.hz,abs(handles.current_data));
        end
        
    otherwise
        
        %do nothing
end

end

guidata(hObject, handles);


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

datanum = round(get(handles.data_selector, 'Value'));
val = get(handles.data2display,'Value');
str = get(handles.data2display,'String');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
numfids = length(handles.data(1,:));

if val >= 4
    
    beep
    errordlg('Can only compare FID, FT, or Processed');
    
else

data2addstr = inputdlg('Enter the data # to add');
data2add = round(str2double(data2addstr));

    if data2add > numfids || data2add < 1
        beep
        errordlg('Not a valid data number!');
        
    else
        
        
        switch str{val}
            case 'FID'
                hold on
                switch dataTag
                    case 'realData'
                        plot(handles.fidtime,real(handles.data(:,data2add)),'k')
                    case 'imagData'
                        plot(handles.fidtime,imag(handles.data(:,data2add)),'k')
                    case 'ReImdata'
                        plot(handles.fidtime,real(handles.data(:,data2add)),'k',...
                        handles.hz,imag(handles.data(:,data2add)),'m');
                    case 'magnitudeData'
                        plot(handles.fidtime,abs(handles.data(:,data2add)),'k');
                end
                hold off
            case 'FT'
                phi = pi*get(handles.phase_slider, 'Value')/180;
                lb = str2double(get(handles.lb, 'String'));
                data2add = phasespectrum(handles.fidtime, handles.data(:,data2add), phi, lb);
                hold on
                switch dataTag
                    case 'realData'
                        plot(handles.hz,real(data2add),'k')
                    case 'imagData'
                        plot(handles.hz,imag(data2add),'k')
                    case 'ReImdata'
                        plot(handles.hz,real(data2add),'k',...
                        handles.hz,imag(data2add),'m');
                    case 'magnitudeData'
                        plot(handles.hz,abs(data2add),'k');
                end
                hold off
        
            case 'Processed'
                hold on
                switch dataTag
                    case 'realData'
                        plot(handles.hz,real(handles.pro(:,data2add)),'k')
                    case 'imagData'
                        plot(handles.hz,imag(handles.pro(:,data2add)),'k')
                    case 'ReImdata'
                        plot(handles.hz,real(handles.pro(:,data2add)),'k',...
                        handles.hz,imag(handles.pro(:,data2add)),'m');
                    case 'magnitudeData'
                        plot(handles.hz,abs(handles.pro(:,data2add)),'k');
                end
                hold off        

        end

    end

end %


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

% Pressing this button calculates the SNR of the processed data for the
% currently selected data number. It will do this even if not looking at
% that particular FT or processed data (i.e. if looking at the integrals
% and you press SNR, the plot will change to the processed data. If no
% processed data exit for that particular data set, it will be created with
% the current settings for phase and lb.

datanum = round(get(handles.data_selector,'Value'));
left = str2double(get(handles.snrleft, 'String'));
right = str2double(get(handles.snrright, 'String'));
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
datacheck = get(handles.data2display, 'Value');

% check that we're looking at a spectrum

if datacheck > 3
    
    beep
    errordlg('What do you want to take the SNR of?')
    
else

% check for auto snr    
    
if isequal(right, 0) && isequal(left, 0)
    set(handles.data2display, 'Value', 3);

% If there is no processed data, make it with current phi and lb    
    
if ~all(handles.pro(:,datanum))
    lb = str2double(get(handles.lb, 'String'));
    handles.linebroad(datanum) = lb;
    phi = pi*get(handles.phase_slider, 'Value')/180;
    handles.phase(datanum) = get(handles.phase_slider, 'Value');

    handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
    
end
    
% Find the peak

peak = max(abs(real(handles.pro(:,datanum))));
peaklocation = find(abs(real(handles.pro(:,datanum))) == peak);

%If user wants auto SNR, find the peak and go 50X past the FWHM (which is
%usually less than actual FWHM). Remove this region from the data leaving
%just the noise.
    
    halflinewidth = find(abs(real(handles.pro(:,datanum))) >= peak/2);
    noise = real(handles.pro(:,datanum));
    index1 = halflinewidth(1) - (peaklocation - halflinewidth(1))*51;
    
    if index1 < 1
        index1 = 1;
    end    
    
    index2 = halflinewidth(end) + (halflinewidth(end)-peaklocation)*51;
    
    if index2 > length(handles.pro(:,datanum))
        index2 = length(handles.pro(:,datanum));
    end
    
    signal = real(handles.pro(index1:index2,datanum));
    noise(index1:index2) = [];
    snr = peak/(2*std(noise));
    handles.snr(datanum) = snr;
    
    set(handles.snr_display, 'String', num2str(snr, '%5.0f'));
    handles.current_data = handles.pro(:,datanum);
    plot(handles.hz, real(handles.current_data));

% If user set regions, check that they are valid, then same as above, but 
% user regions.
    
else
   
   if left < min(handles.hz) || right > max(handles.hz) || left > right
       beep
       errordlg('Come on man! Enter a valid noise region!');
   else
       set(handles.data2display, 'Value', 3);
       
    if ~all(handles.pro(:,datanum))
        lb = str2double(get(handles.lb, 'String'));
        phi = pi*get(handles.phase_slider, 'Value')/180;

        handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
    
    end       
       
    peak = max(abs(real(handles.pro(:,datanum)))); 
    
    index1 = find(handles.hz >= left,1);
    index2 = find(handles.hz <= right, 1, 'last');
   
   noise = real(handles.pro(index1:index2, datanum));
   snr = peak/(2*std(noise));
   handles.snr(datanum) = snr;
   
   set(handles.snr_display, 'String', num2str(snr, '%5.0f'));
   handles.current_data = handles.pro(:,datanum);
   plot(handles.hz, real(handles.current_data));   
   end   

end  

end

guidata(hObject, handles);




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

%See snr_button_Callback for details. The only differences are that this
%function finds the SNR of each spectrum. If processed data already exist I
%use that, if it is empty, I fill it with the current settings.

datanum = round(get(handles.data_selector,'Value'));
left = str2double(get(handles.snrleft, 'String'));
right = str2double(get(handles.snrright, 'String'));
numfids = length(handles.data(1,:));
datacheck = get(handles.data2display, 'Value');

if datacheck > 3
    
    beep
    errordlg('What do you want to take the SNR of?')
    
else

if isequal(right, 0) && isequal(left, 0)
    
for m = 1:numfids
    
    %If there is no processed data for that spectrum, apply current
    %settings.
    if ~all(handles.pro(:,m))
        lb = str2double(get(handles.lb, 'String'));
        handles.linebroad(m) = lb;
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.phase(m) = get(handles.phase_slider, 'Value');

        handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
    end
end
   
for m = 1:numfids

peak = max(abs(real(handles.pro(:,m))));
peaklocation = find(abs(real(handles.pro(:,m))) == peak);

%If user wants auto SNR, find the peak and go 50X past the FWHM (which is
%usually less than actual FWHM). Remove this region from the data and call
%it noise.
    
    halflinewidth = find(abs(real(handles.pro(:,m))) >= peak/2);
    noise = real(handles.pro(:,m));
    index1 = halflinewidth(1) - (peaklocation - halflinewidth(1))*51;
    
    %If the peak is close to the left edge and 50X is too far, make the
    %first point of the data set indes 1.
    if index1 < 1
        index1 = 1;
    end    
    
    index2 = halflinewidth(end) + (halflinewidth(end)-peaklocation)*51;
    
    %If the peak is close to the right edge and 50X is too far, make the
    %last point index 2
    if index2 > length(handles.pro(:,m))
        index2 = length(handles.pro(:,m));
    end
    
    signal = real(handles.pro(index1:index2,m));
    noise(index1:index2) = [];
    handles.snr(m) = peak/(2*std(noise));
    
end %done with loop calculating the auto snr.
    
handles.current_data = handles.snr;
     if ~all(handles.x4columns)
          plot(handles.current_data)
     else
          plot(handles.x4columns, handles.current_data)
     end
set(handles.data2display, 'Value', 6);
else
   
   if left < min(handles.hz) || right > max(handles.hz) || left > right
       beep
       errordlg('Come on man! Enter a valid noise region!');
   else
       
    for m = 1:numfids
    
    %If there is no processed data for that spectrum, apply current
    %settings.
        if ~all(handles.pro(:,m))
         lb = str2double(get(handles.lb, 'String'));
         phi = pi*get(handles.phase_slider, 'Value')/180;

         handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
        end
    end

    for m = 1:numfids
       
        peak = max(abs(real(handles.pro(:,m))));
    
        index1 = find(handles.hz >= left,1);
        index2 = find(handles.hz <= right, 1, 'last');
   
        noise = real(handles.pro(index1:index2, m));
        handles.snr(m) = peak/(2*std(noise));

    end
    handles.current_data = handles.snr;
    
    if ~all(handles.x4columns)
          plot(handles.current_data, 'ko')
    else
          plot(handles.x4columns, handles.current_data, 'ko')
    end
    set(handles.data2display, 'Value', 6);
   
end   

end  

end

guidata(hObject, handles);



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

% Currently I'm doing a 4th degree polynomial fit with two regions chosen 
% by the user. I fit the real and the imaginary parts separately! I assume
% the user is looking at real data. For the real part, simply do a fit on 
% the two defined regions. For the imaginary part, phase shift the data 90
% degress to make the imaginary look like the real. I do this so the
% defined region will be fine and not overlap with the 'wings' of the
% imaginary spectrum. Then I fit and subtract the baseline from the
% shifted imaginary data. Finally, I phase shift it back and replace the
% imaginary portion of the data with the new baseline subtracted one.

% Note two potential errors that can occur when baseline correcting.
% 1) Basesline correct works on processed data and saves the results as
% processed data. Therefore, it is possible to baseline correct multiple
% times which you may or may not want to do. 

% 2) Baseline correction is meant as the last stage of processing. If you
% use one of the apply buttons, the baseline is reset to the original so
% you will need to baseline correct again. This is intentional so that the
% user has a way to reset their spectrum if they are unhappy with a
% baseline correction. 

% I hope a future update will include a way to change the fitting method and
% even inclued splice.

datanum = round(get(handles.data_selector, 'Value'));
left1 = str2double(get(handles.baseline_left1, 'String'));
left2 = str2double(get(handles.baseline_left2, 'String'));
right1 = str2double(get(handles.baseline_right1, 'String'));
right2 = str2double(get(handles.baseline_right2, 'String'));
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');

datacheck = get(handles.data2display, 'Value');

range = [left1 left2 right1 right2];

%check that the defined region is valid, if not, the else down below prints
%an error.

if datacheck > 3
    
    beep
    errordlg('What are you trying to baseline correct?')
    
else
    
if left1 < left2 && left2 < right1 && right1 < right2 && min(handles.hz)...
        <= min(range) && max(handles.hz) > max(range)

set(handles.data2display, 'Value', 3);

index1 = find(handles.hz >= left1, 1);
index2 = find(handles.hz <= left2, 1, 'last');
index3 = find(handles.hz >= right1, 1);
index4 = find(handles.hz <= right2, 1, 'last');

x1 = handles.hz(index1:index2);
x2 = handles.hz(index3:index4);
xdata = [x1 x2]';

%check to see if there is already processed data. If so, baseline correct
%the processed data ignoring any unprocessed phase and lb changes.
if any(handles.pro(:,datanum))
    phi = handles.phase(datanum)*pi/180;
    lb = handles.linebroad(datanum);
    phi_i = phi+pi/2;   % used to 'phase' the imaginary data and then repeat the background subrtaction on it.
    imaginary_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi_i, lb);
    y1 = real(handles.pro(index1:index2,datanum));
    y2 = real(handles.pro(index3:index4,datanum));
    y1_i = imag(imaginary_data(index1:index2))';
    y2_i = imag(imaginary_data(index3:index4))';
    ydata = [y1; y2];
    yi_data = [y1_i; y2_i];
    
%If handles.pro was empty, process the current data set with the given lb and phase.
else
    
    lb = str2double(get(handles.lb, 'String'));
    handles.linebroad(datanum) = lb;
    phi = pi*get(handles.phase_slider, 'Value')/180;
    handles.phase(datanum) = phi*180/pi;
    phi_i = phi+pi/2;
    
    handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
    imaginary_data = phasespectrum(handles.fidtime, handles.data(:,datanum), phi_i, lb);    
           
    y1 = real(handles.pro(index1:index2,datanum));
    y2 = real(handles.pro(index3:index4,datanum));
    y1_i = imag(imaginary_data(index1:index2))';
    y2_i = imag(imaginary_data(index3:index4))';
    ydata = [y1; y2];
    yi_data = [y1_i; y2_i];
    
end

warning off all
cv = polyfit(xdata, ydata, 4);
cvi = polyfit(xdata, yi_data, 4);
% I will need to phase shift the imaginary data back before subtracting

hz = handles.hz;
warning on all

plot(hz, real(handles.pro(:,datanum)), hz, polyval(cv, hz)')

docorrection = menu('Apply Baseline Correction?', 'Ok', 'No way');

    % If user chose to do the correction, apply it, otherwise set current
    % data to the currently processed spectrum.
    if isequal(docorrection, 1)
        bcImData = imaginary_data - 1i*polyval(cvi, hz); %Subtract off the imaginary fit
        bc_fidImData = ifft(ifftshift(bcImData)); %ifft to go back to time domain
        correctPhaseImData = phasespectrum(handles.fidtime, bc_fidImData, -pi/2, 0);  %phase shift it back
        handles.pro(:, datanum) = real(handles.pro(:,datanum)) - polyval(cv, hz)' + 1i*imag(correctPhaseImData)';

        handles.current_data = handles.pro(:, datanum);

        plot(handles.hz, real(handles.current_data));
    else
    
        handles.current_data = handles.pro(:, datanum);

        plot(handles.hz, real(handles.current_data));
    end

    if ~strcmp(dataTag, 'realData')
        set(handles.realData, 'Value', 1);
        set(handles.imagData, 'Value', 0);
        set(handles.ReImdata, 'Value', 0);
        set(handles.magnitudeData, 'Value', 0);
    end
    
else %else if region is invalid, give an error
    
    beep
    errordlg('Invalid regions for baseline correction.') 

end %end the if that checks for valid region.
end %end checking to make sure data2display was valid
guidata(hObject, handles);


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

% Currently I'm doing a 4th degree polynomial fit with two regions chosen 
% by the user. See baseline 1 for notes. 

datanum = round(get(handles.data_selector, 'Value'));
left1 = str2double(get(handles.baseline_left1, 'String'));
left2 = str2double(get(handles.baseline_left2, 'String'));
right1 = str2double(get(handles.baseline_right1, 'String'));
right2 = str2double(get(handles.baseline_right2, 'String'));
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
numfids = length(handles.data(1,:));

datacheck = get(handles.data2display, 'Value');

range = [left1 left2 right1 right2];

%check that the defined region is valid, if not, the else down below prints
%an error.

if datacheck > 3
    
    beep
    errordlg('What are you trying to baseline correct?')
    
else
    
if left1 < left2 && left2 < right1 && right1 < right2 && min(handles.hz)...
        <= min(range) && max(handles.hz) > max(range)

set(handles.data2display, 'Value', 3);

index1 = find(handles.hz >= left1, 1);
index2 = find(handles.hz <= left2, 1, 'last');
index3 = find(handles.hz >= right1, 1);
index4 = find(handles.hz <= right2, 1, 'last');

x1 = handles.hz(index1:index2);
x2 = handles.hz(index3:index4);
xdata = [x1 x2]';

%check to see if there is already processed data. If so, baseline correct
%the processed data ignoring any unprocessed phase and lb changes.

for m = 1:numfids

if any(handles.pro(:,m))
    
    y1 = real(handles.pro(index1:index2,m));
    y2 = real(handles.pro(index3:index4,m));
    
    phi = handles.phase(m)*pi/180;
    lb = handles.linebroad(m);
    phi_i = phi+pi/2;   % used to 'phase' the imaginary data and then repeat the background subrtaction on it.    
    imaginary_data = phasespectrum(handles.fidtime, handles.data(:,m), phi_i, lb); 
    y1_i = imag(imaginary_data(index1:index2))';
    y2_i = imag(imaginary_data(index3:index4))';
    ydata = [y1; y2];
    yi_data = [y1_i; y2_i;];
    
%If handles.pro was empty, process the current data set with the given lb and phase.
else
    
    lb = str2double(get(handles.lb, 'String'));
    handles.linebroad(m) = lb;
    phi = pi*get(handles.phase_slider, 'Value')/180;
    handles.phase(m) = phi*180/pi;
    phi_i = phi+pi/2;
    handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
    imaginary_data = phasespectrum(handles.fidtime, handles.data(:,m), phi_i, lb);        
    y1 = real(handles.pro(index1:index2,m));
    y2 = real(handles.pro(index3:index4,m));
    y1_i = imag(imaginary_data(index1:index2))';
    y2_i = imag(imaginary_data(index3:index4))';
    ydata = [y1; y2];
    yi_data = [y1_i; y2_i;];
end

%This is a fit function I generated code from in cftool
%[cv] = poly4baseline(xdata, ydata);
warning off all
cv = polyfit(xdata, ydata, 4);
cvi = polyfit(xdata, yi_data, 4);
hz = handles.hz;

bcImData = imaginary_data - 1i*polyval(cvi, hz); %Subtract off the imaginary fit
bc_fidImData = ifft(ifftshift(bcImData)); %ifft to go back to time domain
correctPhaseImData = phasespectrum(handles.fidtime, bc_fidImData, -pi/2, 0);  %phase shift it back
handles.pro(:, m) = real(handles.pro(:,m)) - polyval(cv, hz)' + 1i*imag(correctPhaseImData)';

end

if ~strcmp(dataTag, 'realData')
    set(handles.realData, 'Value', 1);
    set(handles.imagData, 'Value', 0);
    set(handles.ReImdata, 'Value', 0);
    set(handles.magnitudeData, 'Value', 0);
end
handles.current_data = handles.pro(:,datanum);
plot(handles.hz, real(handles.current_data));

else %else if region is invalid, give an error
    
    beep
    errordlg('Invalid regions for baseline correction.') 

end %end the if that checks for valid region.
end %end checking to make sure data2display was valid
warning on all
guidata(hObject, handles);

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

% This function gets the integral AND the peak amplitude of the given data
% set. It displays the integral value.

datanum = round(get(handles.data_selector, 'Value'));
x1 = str2double(get(handles.intleft, 'String'));
x2 = str2double(get(handles.intright, 'String'));
datacheck = get(handles.data2display, 'Value');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');

if datacheck == 1 || datacheck > 3

    beep
    errordlg('What are you trying to integrate?')
    
else
    
    if ~all(handles.pro(:,datanum))
        lb = str2double(get(handles.lb, 'String'));
        handles.linebroad(datanum) = lb;
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.phase(datanum) = phi*180/pi;
        handles.pro(:,datanum) = phasespectrum(handles.fidtime, handles.data(:,datanum), phi, lb);
    end
        
    if isequal(x1, 0) && isequal(x2, 0)
        
        switch dataTag
            case 'realData'
                handles.integrals(datanum) = trapz(real(handles.pro(:,datanum)));
                maxmag = max(abs(real(handles.pro(:,datanum))));
                if maxmag == max(real(handles.pro(:,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end
            case 'imagData'
                handles.integrals(datanum) = trapz(imag(handles.pro(:,datanum)));
                maxmag = max(abs(imag(handles.pro(:,datanum))));
                if maxmag == max(imag(handles.pro(:,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end                
            case 'ReImdata'
                handles.integrals(datanum) = trapz(real(handles.pro(:,datanum)));
                maxmag = max(abs(real(handles.pro(:,datanum))));
                if maxmag == max(real(handles.pro(:,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end                
            case 'magnitudeData'
                handles.integrals(datanum) = trapz(abs(handles.pro(:,datanum)));
                handles.peaks(datanum) = max(abs(handles.pro(:,datanum)));
        end
        outputstr = sprintf('Integral is %5.5g', handles.integrals(datanum));
        msgbox(outputstr)
            
    elseif x1 < min(handles.hz) || x2 > max(handles.hz) || x2 < x1
            
         beep
         errordlg('Invalid integration region')
            
    else
        index1 = find(handles.hz >= x1,1);
        index2 = find(handles.hz <= x2, 1, 'last');
        switch dataTag
            case 'realData'
                handles.integrals(datanum) = trapz(real(handles.pro(index1:index2,datanum)));
                maxmag = max(abs(real(handles.pro(index1:index2,datanum))));
                if maxmag == max(real(handles.pro(index1:index2,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end                
            case 'imagData'
                handles.integrals(datanum) = trapz(imag(handles.pro(index1:index2,datanum)));
                maxmag = max(abs(imag(handles.pro(index1:index2,datanum))));
                if maxmag == max(imag(handles.pro(index1:index2,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end                
            case 'ReImdata'
                handles.integrals(datanum) = trapz(real(handles.pro(index1:index2,datanum)));
                maxmag = max(abs(real(handles.pro(index1:index2,datanum))));
                if maxmag == max(real(handles.pro(index1:index2,datanum)))
                    handles.peaks(datanum) = maxmag;
                else
                    handles.peaks(datanum) = -maxmag;
                end                
            case 'magnitudeData'
                handles.integrals(datanum) = trapz(abs(handles.pro(index1:index2,datanum)));
                handles.peaks = max(abs(handles.pro(:,datanum)));           
        end        
        outputstr = sprintf('Integral is %5.5g', handles.integrals(datanum));
        msgbox(outputstr)

    end
end
             

guidata(hObject, handles);

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

x1 = str2double(get(handles.intleft, 'String'));
x2 = str2double(get(handles.intright, 'String'));
index1 = find(handles.hz >= x1,1);
index2 = find(handles.hz <= x2, 1, 'last');
datacheck = get(handles.data2display, 'Value');
data2show = get(handles.realorimaginary, 'SelectedObject');
dataTag = get(data2show,'Tag');
numfids = length(handles.data(1,:));

if datacheck == 1 || datacheck > 3

    beep
    errordlg('What are you trying to integrate?')
    
else
    
  for m = 1:numfids 
      
    if ~all(handles.pro(:,m))
        lb = str2double(get(handles.lb, 'String'));
        handles.linebroad(m) = lb;
        phi = pi*get(handles.phase_slider, 'Value')/180;
        handles.phase(m) = phi*180/pi;
        handles.pro(:,m) = phasespectrum(handles.fidtime, handles.data(:,m), phi, lb);
    end
    
    if isequal(x1, 0) && isequal(x2, 0)
        switch dataTag
            case 'realData'
                maxmag = max(abs(real(handles.pro(:,m))));
        
                    if maxmag == max(real(handles.pro(:,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'imaginaryData'
                 maxmag = max(abs(imag(handles.pro(:,m))));
        
                    if maxmag == max(imag(handles.pro(:,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'ReImdata'
                maxmag = max(abs(real(handles.pro(:,m))));
        
                    if maxmag == max(real(handles.pro(:,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'magnitudeData'
                maxmag = max(abs(handles.pro(:,m)));
        
        end         
    else
        switch dataTag
            case 'realData'
                maxmag = max(abs(real(handles.pro(index1:index2,m))));
        
                    if maxmag == max(real(handles.pro(index1:index2,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'imaginaryData'
                 maxmag = max(abs(imag(handles.pro(index1:index2,m))));
        
                    if maxmag == max(imag(handles.pro(index1:index2,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'ReImdata'
                maxmag = max(abs(real(handles.pro(index1:index2,m))));
        
                    if maxmag == max(real(handles.pro(:,m)))
                        handles.peaks(m) = maxmag;
                    else
                        handles.peaks(m) = -maxmag;
                    end
            case 'magnitudeData'
                handles.peaks(m) = max(abs(handles.pro(index1:index2,m)));
        
        end     
    end
    
  end
  
  
    if isequal(x1, 0) && isequal(x2, 0)
          
        switch dataTag
            case 'realData'
                handles.integrals = trapz(real(handles.pro));
            case 'imagData'
                handles.integrals = trapz(imag(handles.pro));
            case 'ReImdata'
                handles.integrals = trapz(real(handles.pro));
            case 'magnitudeData'
                handles.integrals = trapz(abs(handles.pro));
        end
        handles.current_data = handles.integrals;
        set(handles.data2display, 'Value', 4);
        
        if any(handles.x4columns)   
            plot(handles.x4columns, handles.current_data, 'ko');    
        else    
            plot(handles.current_data, 'ko');    
        end
        
    elseif x1 < min(handles.hz) || x2 > max(handles.hz) || x2 < x1
            
         beep
         errordlg('Invalid integration region')
            
    else
        

        switch dataTag
            case 'realData'
                handles.integrals = trapz(real(handles.pro(index1:index2,:)));
            case 'imagData'
                handles.integrals = trapz(imag(handles.pro(index1:index2,:)));
            case 'ReImdata'
                handles.integrals = trapz(real(handles.pro(index1:index2,:)));
            case 'magnitudeData'
                handles.integrals = trapz(abs(handles.pro(index1:index2,:)));
        end   
        
        handles.current_data = handles.integrals;
        set(handles.data2display, 'Value', 4);       
        
        if any(handles.x4columns)   
            plot(handles.x4columns, handles.current_data, 'ko');    
        else    
            plot(handles.current_data, 'ko');    
        end
        
    end
end
             

guidata(hObject, handles);

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

% Reads in user data

xdataname = inputdlg('Enter the ROW vector variable containing the x-data (0 to cancel)');
ws_vars = evalin('base', 'who');

if isequal(str2double(xdataname{1}),0)
    
    %do nothing
    
elseif ismember(xdataname{1},ws_vars)
  usrvar = evalin('base', xdataname{1});
  
  if isequal(size(usrvar), size(handles.x4columns))
      handles.x4columns = usrvar;
      
  else
      beep
      errordlg('Incorrect variable dimensions')
  end
  
else
    
    errordlg('Variable does not exist')
    
end

guidata(hObject, handles);

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

% This function normalizes both the integral and peaks data. It therefore
% overwrites the currently saved values with the new ones.

numfids = length(handles.data(1,:));
inputdata = inputdlg({'What data number do you want to set the value of?',...
    'What value do you want to normalize to?'});
datanum = str2double(inputdata{1});
value = str2double(inputdata{2});

if datanum <1 || datanum > numfids
        
      beep
      errordlg('Not a valid data number')
       
 else
        
      newints = handles.integrals/handles.integrals(datanum)*value;
      handles.integrals = newints;
        
      newpeaks = handles.peaks/handles.peaks(datanum)*value;
      handles.peaks = newpeaks;
        
      set(handles.data2display, 'Value', 4)
      handles.current_data = handles.integrals;
        
      if any(handles.x4columns)   
          plot(handles.x4columns, handles.current_data, 'ko');    
      else    
          plot(handles.current_data, 'ko');    
      end
    
end
guidata(hObject, handles);
        
% --- Executes on button press in do_fit.
function do_fit_Callback(hObject, eventdata, handles) %#ok<*DEFNU,*INUSL>
% hObject    handle to do_fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Currently will do 1 of 3 different types of fits for the peaks or the
%integrals. maxsig is used for an initial guess in the fit since if the
%data is not normalized it may have a hard time fitting.

intorpeak = get(handles.data2fit, 'Value');
fitfunction = get(handles.fitfunction, 'Value');
numfids = length(handles.data(1,:));

%Bring up a menu to find out what data to fit.

datarange = menu('Choose Data to Fit', 'All', 'Select Range');

if isequal(datarange, 2)
    
    indexes = inputdlg({'Enter index of 1st data point to fit', 'Enter index of 2nd data point to fit'});
    index1 = str2num(indexes{1});
    index2 = str2num(indexes{2});
    
end


if ~any(handles.x4columns)
    
  %Notify user no x-data exist.
    menu('No X-data has been imported for fitting. Will use index number.', 'OK');
end

% Get our X and Y data based on 1) If integrals or peaks was selected. 2)
% X-data has already been imported or not, 3) Whether or not we should fit
% all the data or just a range.

if isequal(intorpeak, 1) %integrals)
    
    maxsig = max(handles.integrals);
    minsig = min(handles.integrals);
            
    if ~any(handles.x4columns)
         if isequal(datarange, 1)
             ydata = handles.integrals;
             xdata = (1:1:numfids);
         else
             xdata = (index1:1:index2);
             ydata = handles.integrals(index1:index2);
         end
                
    else
         if isequal(datarange, 1)
             xdata = handles.x4columns;
             ydata = handles.integrals;
         else
             xdata = handles.x4columns(index1:index2);
             ydata = handles.integrals(index1:index2);
         end
           
    end
            
            
else      
  
    maxsig = max(handles.peaks);
    minsig = min(handles.peaks);
    
    if ~any(handles.x4columns)
         if isequal(datarange, 1)
             xdata = (1:1:numfids);
             ydata = handles.peaks;
         else
             xdata = (index1:1:index2);
             ydata = handles.peaks(index1:index2);
         end
                
    else
         if isequal(datarange,1)
             xdata = handles.x4columns;
             ydata = handles.peaks;
         else
             xdata = handles.x4columns(index1:index2);
             ydata = handles.peaks(index1:index2);
         end

     end
end

% Now that we have our data, do the desired fit.

switch fitfunction
    
    case 1 %T1 inversion recovery


        %Note that time needs to be in ms for the initial guess to be a good
        %one...maybe I need to have menu to prompt so the user knows?

        s = fitoptions('Method', 'NonlinearLeastSquares','Lower',[0 0 0],...
            'Upper', [5*maxsig 10 600000], 'Startpoint', [maxsig 1 500]);

        f = fittype('Io*(1-2*A*exp(-x/T1))', 'coefficients',...
            {'Io', 'A', 'T1'},'options',s);

        [fitres, gof] = fit(xdata', ydata',f);

        ci = confint(fitres, 0.68);     %~ 1 SD

        Iosd = ci(2,1) - fitres.Io;
        Asd = ci(2,2) - fitres.A;
        T1sd = ci(2,3) - fitres.T1;
        
    
% Decide what to plot. If data range is 'All', then the variables xdata,
% and ydata already have the desired data to plot. But if data range was
% not 'All', I want to still plot all the data, but just show the fit in
% the range that was specified. So find if we were fitting integrals or
% peaks and if x-data has been imported or not.
    
       if isequal(datarange, 1)
          plot(xdata, ydata, 'ko')
          hold on
          plot(fitres,'r')
          hold off
        else
            if isequal(intorpeak, 1)
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.integrals, 'ko')
                    hold on
                    plot(fitres,'r')

                else
                    plot(handles.x4columns, handles.integrals, 'ko')
                    hold on
                    plot(fitres,'r')
                   
                end
            else
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.peaks, 'ko')
                    hold on
                    plot(fitres,'r')
                 
                else
                    plot(handles.x4columns, handles.peaks, 'ko')
                    hold on
                    plot(fitres,'r')
            
                end   
            end
        end
         
        str1 = sprintf('Fit Eqn = Io*(1-2*A*exp(-t/T1))');
        str2 = sprintf('\n\nT1 = %0.3f (%0.3f)', fitres.T1, T1sd);
        str3 = sprintf('\n\nIo = %0.3f (%0.3f)',fitres.Io, Iosd);
        str4 = sprintf('\n\nA = %0.3f (%0.3f)',fitres.A, Asd);
        
        outputstr = strcat(str1, str2, str3, str4);
        
        
    case 2 %flip angle
        
    % First try to estimate a decent initial condition for w in sin(w*x) because it could vary
    % by several orders of magnitude depinding on what the user has done. I
    % assume the x-axis spans between 1/2 to 1 period and make an initial
    % guess for w based on that assumption.
    
    x90str = inputdlg('Enter the value for an approximate 90 pulse');
    x90 = str2double(x90str);
    eqn = sprintf('sin(x*%f) = 1',x90);
    winit = double(solve(eqn));
%     if ~all(handles.x4columns)
%         winit = pi/numfids;
%     else
%         winit = pi/max(handles.x4columns);
%     end
        
    s = fitoptions('Method', 'NonlinearLeastSquares','Lower',[0 0 -2*pi minsig],...
            'Upper', [5*maxsig 10000 2*pi maxsig], 'Startpoint', [maxsig/2 winit 0 0]);

    f = fittype('A*sin(w*x + phi) +C', 'coefficients',{'A', 'w', 'phi', 'C'},'options',s);
    
    [fitres, gof] = fit(xdata', ydata',f);

    ci = confint(fitres, 0.68);     %~ 1 SD

    Asd = ci(2,1) - fitres.A;
    wsd = ci(2,2) - fitres.w;
    phisd = ci(2,3) - fitres.phi;
    Csd = ci(2,4) - fitres.C;
        
        
    
        if isequal(datarange, 1)
            plot(xdata, ydata, 'ko')
            hold on
            plot(fitres,'r')
        else
            if isequal(intorpeak, 1)
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.integrals, 'ko')
                    hold on
                    plot(fitres,'r')
                else
                    plot(handles.x4columns, handles.integrals, 'ko')
                    hold on
                    plot(fitres,'r')
                end
            else
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.peaks, 'ko')
                    hold on
                    plot(fitres,'r')
                else
                    plot(handles.x4columns, handles.peaks, 'ko')
                    hold on
                    plot(fitres,'r')
                end   
            end
        end    
    
    hold off       
    str1 = sprintf('Fit Eqn = A*sin(w*x + phi) + C');
    str2 = sprintf('\n\nA = %0.2f (%0.2f)', fitres.A, Asd);
    str3 = sprintf('\n\nw = %0.3f (%0.4f)',fitres.w, wsd);
    str4 = sprintf('\n\nphi = %0.2f (%0.2f)',fitres.phi, phisd);
    str5 = sprintf('\n\nC = %0.2f (%0.3f)',fitres.C, Csd);
    outputstr = strcat(str1, str2, str3, str4, str5);        
        
    case 3 %E vs P
  

    s = fitoptions('Method', 'NonlinearLeastSquares','Lower',[-20*maxsig 0 0],...
            'Upper', [20*maxsig 1000 1000], 'Startpoint', [maxsig -minsig -minsig/10]);
        
    f = fittype('Io-A*x/(1+B*x)', 'coefficients', {'Io','A','B'},'options',s);

    [fitres, gof] = fit(xdata', ydata',f);

    ci = confint(fitres, 0.68);     %~ 1 SD

    Iosd = ci(2,1) - fitres.Io;
    Asd = ci(2,2) - fitres.A;
    Bsd = ci(2,3) - fitres.B;
    emax = fitres.Io - fitres.A/fitres.B;
    emaxSD = (abs(emax)+1)*sqrt((Asd/fitres.A)^2 + (Bsd/fitres.B)^2);
    
        if isequal(datarange, 1)
            plot(xdata, ydata, 'ko')
            hold on
            plot(fitres, 'r')
        else
            if isequal(intorpeak, 1)
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.integrals, 'ko')
                    hold on
                    plot(fitres, 'r')

                else
                    plot(handles.x4columns, handles.integrals, 'ko')
                    hold on
                    plot(fitres, 'r')
                end
            else
                if ~any(handles.x4columns)
                    plot((1:1:numfids), handles.peaks, 'ko')
                    hold on
                    plot(fitres, 'r')
                else
                    plot(handles.x4columns, handles.peaks, 'ko')
                    hold on
                    plot(fitres, 'r')
                end   
            end
        end    
    hold off
    
    str1 = sprintf('Fit Eqn = Io - A*P/(1 + B*P)');
    str2 = sprintf('\n\nEmax = %0.2f (%0.2f)', emax, emaxSD);
    str3 = sprintf('\n\nIo = %0.2f (%0.2f)',fitres.Io, Iosd);
    str4 = sprintf('\n\nA = %0.2f (%0.2f)',fitres.A, Asd);
    str5 = sprintf('\n\nB = %0.2f (%0.3f)',fitres.B, Bsd);
    outputstr = strcat(str1, str2, str3, str4, str5);      
        
end
        
handles.FitResults = fitres;
handles.GOF = gof;
msgbox(outputstr, 'Fit Parameters');
         
guidata(hObject, handles);


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

% Send selected data to the workspace

datatype = get(handles.data2export, 'Value');
numfids = length(handles.data(1,:));

if datatype <= 7
    namecell = inputdlg('Type a variable name for the workspace');
    name1 = namecell{1};
    name2 = ' ';
end

if isequal(datatype, 8)
    namecell = inputdlg({'Enter name for Results object','Enter a name for gof structure'});
    name1 = namecell{1};
    name2 = namecell{2};
end

ws_vars = evalin('base', 'who');

%If the variable name exist already, ask the user if they want to overwrite
%it. If the variable does not exist, set writedata to 'y' so we write the
%data to the workspace.

if ismember(name1,ws_vars) || ismember(name2, ws_vars)
  
    writedata = inputdlg('Variable name already exist. Overwrite? (y/n)');
    
else
    
    writedata = 'y';
    
end

if strcmpi(writedata, 'y')
    

switch datatype
    
    case 1 %FID
        
         assignin('base', name1, handles.data);
         
    case 2 % FT
        ftdata = zeros(size(handles.data));
        for m = 1:numfids
            ftdata(:,m) = fftshift(fft(handles.data(:,m)));
        end
            
        assignin('base', name1, ftdata);
            
    case 3 %Processed
        
        assignin('base', name1, handles.pro);
        
    case 4 %Integrals
        
        assignin('base', name1, handles.integrals);
        
    case 5 %Peaks
        
        assignin('base', name1, handles.peaks);
        
    case 6 %SNR
        
        assignin('base', name1, handles.snr);
        
    case 7 %Applied Phase
        
        assignin('base', name1, handles.phase);
        
    case 8 %Last Fit Coeffecients
        
        assignin('base', name1, handles.FitResults);
        assignin('base', name2, handles.GOF);
end

else
 
    %do nothing because we don't want to overwrite existing variable
    
end


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



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



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



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

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

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

function snrright_Callback(hObject, eventdata, handles) %#ok<*INUSD>
% hObject    handle to snrright (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 snrright as text
%        str2double(get(hObject,'String')) returns contents of snrright as a double

% --- Executes during object creation, after setting all properties.
function baseline_left1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to baseline_left1 (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 during object creation, after setting all properties.
function baseline_left2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to baseline_left2 (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 during object creation, after setting all properties.
function baseline_right1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to baseline_right1 (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 during object creation, after setting all properties.
function baseline_right2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to baseline_right2 (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 during object creation, after setting all properties.
function data2display_CreateFcn(hObject, eventdata, handles)
% hObject    handle to data2display (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 during object creation, after setting all properties.
function snr_display_CreateFcn(hObject, eventdata, handles)
% hObject    handle to snr_display (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 during object creation, after setting all properties.
function phase_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to phase_slider (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes during object creation, after setting all properties.
function phaseangle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to phaseangle (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 during object creation, after setting all properties.
function lb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lb (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 during object creation, after setting all properties.
function data_selector_CreateFcn(hObject, eventdata, handles)
% hObject    handle to data_selector (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

% --- Executes during object creation, after setting all properties.
function datanum_display_CreateFcn(hObject, eventdata, handles)
% hObject    handle to datanum_display (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 during object creation, after setting all properties.
function snrleft_CreateFcn(hObject, eventdata, handles)
% hObject    handle to snrleft (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 during object creation, after setting all properties.
function snrright_CreateFcn(hObject, eventdata, handles)
% hObject    handle to snrright (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



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


% --- Executes during object creation, after setting all properties.
function intright_CreateFcn(hObject, eventdata, handles)
% hObject    handle to intright (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



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


% --- Executes during object creation, after setting all properties.
function intleft_CreateFcn(hObject, eventdata, handles)
% hObject    handle to intleft (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 selection change in fitfunction.
function fitfunction_Callback(hObject, eventdata, handles)
% hObject    handle to fitfunction (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


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

% Hint: popupmenu 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 selection change in data2fit.
function data2fit_Callback(hObject, eventdata, handles)
% hObject    handle to data2fit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


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

% Hint: popupmenu 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 selection change in data2export.
function data2export_Callback(hObject, eventdata, handles)
% hObject    handle to data2export (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

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


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

% Hint: popupmenu 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 when selected object is changed in realorimaginary.
function realorimaginary_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in realorimaginary 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

dataTag = get(eventdata.NewValue,'Tag');
val = get(handles.data2display,'Value');

% If val is 1, 2, or 3 (FID, FT, or Processed) update the plot, otherwise
% do nothing
if isequal(val,1)
        
        switch dataTag
            case 'realData' 
                plot(handles.fidtime, real(handles.current_data));
            case 'imagData'
                plot(handles.fidtime, imag(handles.current_data), 'r');
            case 'ReImdata'
                plot(handles.fidtime, real(handles.current_data),'b',...
                    handles.fidtime, imag(handles.current_data), 'r');
            case 'magnitudeData'
                plot(handles.fidtime, abs(handles.current_data));
        end
        
elseif isequal(val,2)||isequal(val,3)
    
      switch dataTag
            case 'realData' 
                plot(handles.hz, real(handles.current_data));
            case 'imagData'
                plot(handles.hz, imag(handles.current_data), 'r');
            case 'ReImdata'
                plot(handles.hz, real(handles.current_data),'b',...
                    handles.hz, imag(handles.current_data), 'r');
            case 'magnitudeData'
                plot(handles.hz, abs(handles.current_data));
        end
        
end
guidata(hObject, handles);
        


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

Contact us