function imdb = alot_get_database(alotDir, varargin)
opts.seed = 0 ;
opts.version = 'grey2';
opts = vl_argparse(opts, varargin) ;

rng(opts.seed, 'twister');

imdb.imageDir = fullfile(alotDir, opts.version) ;
imdb.maskDir = fullfile(alotDir, 'mask'); % DO NOT USE

cats = dir(imdb.imageDir);
cats = cats([cats.isdir] & ~ismember({cats.name}, {'.','..'})) ;
imdb.classes.name = {cats.name} ;
imdb.images.id = [] ;

for c=1:numel(cats)
  ims = dir(fullfile(imdb.imageDir, imdb.classes.name{c}, '*.png'));
  %imdb.images.name{c} = fullfile(imdb.classes.name{c}, {ims.name}) ;
  imdb.images.name{c} = cellfun(@(S) fullfile(imdb.classes.name{c}, S), ...
    {ims.name}, 'Uniform', 0);
  imdb.images.label{c} = c * ones(1,numel(ims)) ;
  if numel(ims) ~= 100, error('ops') ; end
  % http://cmp.felk.cvut.cz/~sulcmila/papers/Sulc-TR-2014-12.pdf
  % uses 20 for training, 80 for testing, per class
  sets = [1 * ones(1,20), 3 * ones(1,80)];
  imdb.images.set{c} = sets(randperm(100)) ;
end
imdb.images.name = horzcat(imdb.images.name{:}) ;
imdb.images.label = horzcat(imdb.images.label{:}) ;
imdb.images.set = horzcat(imdb.images.set{:}) ;
imdb.images.id = 1:numel(imdb.images.name) ;

imdb.segments = imdb.images ;
imdb.segments.imageId = imdb.images.id ;
imdb.segments.mask = strrep(imdb.images.name, 'image', 'mask') ;

% make this compatible with the OS imdb
imdb.meta.classes = imdb.classes.name ;
imdb.meta.inUse = true(1,numel(imdb.meta.classes)) ;
imdb.segments.difficult = false(1, numel(imdb.segments.id)) ;


function history(dat)
% Display history for image_vector object

u = '_________________________________________________';
disp(u)
disp('History for image data object')
disp(u)

if ischar(dat.dat_descrip), fprintf('Data description:\n%s\n', dat.dat_descrip); end

disp(char(dat.history))

disp(u)

end


%kxprism 'Interactive 2D & 3D Plotting'
% This MatLab function was automatically generated by a converter (KhorosToMatLab) from the Khoros xprism.pane file
%
% Parameters:
% InputFile: i1 'Input File 1', optional: 'Input plot 1'
% InputFile: i2 'Input File 2', optional: 'Input plot 2'
% InputFile: i3 'Input File 3', optional: 'Input plot 3'
% InputFile: i4 'Input File 4', optional: 'Input plot 4'
% Double: update 'Update time', default: 1: 'How often to check input file for modification'
%
% Example: kxprism({i1, i2, i3, i4}, {'i1','';'i2','';'i3','';'i4','';'update',1})
%
% Khoros helpfile follows below:
%
%  PROGRAM
% xprism - Interactive 2D & 3D Plotting
%
%  DESCRIPTION
%
%
%
%  EXAMPLES
%
%  "SEE ALSO"
%
%  RESTRICTIONS
%
%  REFERENCES
%
%  COPYRIGHT
% Copyright (C) 1993 - 1997, Khoral Research, Inc. ("KRI")  All rights reserved.
%

function varargout = kxprism(varargin)
if nargin ==0
  Inputs={};arglist={'',''};
elseif nargin ==1
  Inputs=varargin{1};arglist={'',''};
elseif nargin ==2
  Inputs=varargin{1}; arglist=varargin{2};
else error('Usage: [out1,..] = kxprism(Inputs,arglist).');
end
if size(arglist,2)~=2
  error('arglist must be of form {''ParameterTag1'',value1;''ParameterTag2'',value2}')
 end
narglist={'i1', '__input';'i2', '__input';'i3', '__input';'i4', '__input';'update', 1};
maxval={1,1,1,1,1};
minval={1,1,1,1,1};
istoggle=[1,1,1,1,1];
was_set=istoggle * 0;
paramtype={'InputFile','InputFile','InputFile','InputFile','Double'};
% identify the input arrays and assign them to the arguments as stated by the user
if ~iscell(Inputs)
Inputs = {Inputs};
end
NumReqOutputs=0; nextinput=1; nextoutput=1;
  for ii=1:size(arglist,1)
  wasmatched=0;
  for jj=1:size(narglist,1)
   if strcmp(arglist{ii,1},narglist{jj,1})  % a given argument was matched to the possible arguments
     wasmatched = 1;
     was_set(jj) = 1;
     if strcmp(narglist{jj,2}, '__input')
      if (nextinput > length(Inputs))
        error(['Input ' narglist{jj,1} ' has no corresponding input!']);
      end
      narglist{jj,2} = 'OK_in';
      nextinput = nextinput + 1;
     elseif strcmp(narglist{jj,2}, '__output')
      if (nextoutput > nargout)
        error(['Output nr. ' narglist{jj,1} ' is not present in the assignment list of outputs !']);
      end
      if (isempty(arglist{ii,2}))
        narglist{jj,2} = 'OK_out';
      else
        narglist{jj,2} = arglist{ii,2};
      end

      nextoutput = nextoutput + 1;
      if (minval{jj} == 0)
         NumReqOutputs = NumReqOutputs - 1;
      end
     elseif isstr(arglist{ii,2})
      narglist{jj,2} = arglist{ii,2};
     else
        if strcmp(paramtype{jj}, 'Integer') & (round(arglist{ii,2}) ~= arglist{ii,2})
            error(['Argument ' arglist{ii,1} ' is of integer type but non-integer number ' arglist{ii,2} ' was supplied']);
        end
        if (minval{jj} ~= 0 | maxval{jj} ~= 0)
          if (minval{jj} == 1 & maxval{jj} == 1 & arglist{ii,2} < 0)
            error(['Argument ' arglist{ii,1} ' must be bigger or equal to zero!']);
          elseif (minval{jj} == -1 & maxval{jj} == -1 & arglist{ii,2} > 0)
            error(['Argument ' arglist{ii,1} ' must be smaller or equal to zero!']);
          elseif (minval{jj} == 2 & maxval{jj} == 2 & arglist{ii,2} <= 0)
            error(['Argument ' arglist{ii,1} ' must be bigger than zero!']);
          elseif (minval{jj} == -2 & maxval{jj} == -2 & arglist{ii,2} >= 0)
            error(['Argument ' arglist{ii,1} ' must be smaller than zero!']);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} < minval{jj})
            error(['Argument ' arglist{ii,1} ' must be bigger than ' num2str(minval{jj})]);
          elseif (minval{jj} ~= maxval{jj} & arglist{ii,2} > maxval{jj})
            error(['Argument ' arglist{ii,1} ' must be smaller than ' num2str(maxval{jj})]);
          end
        end
     end
     if ~strcmp(narglist{jj,2},'OK_out') &  ~strcmp(narglist{jj,2},'OK_in')
       narglist{jj,2} = arglist{ii,2};
     end
   end
   end
   if (wasmatched == 0 & ~strcmp(arglist{ii,1},''))
        error(['Argument ' arglist{ii,1} ' is not a valid argument for this function']);
   end
end
% match the remaining inputs/outputs to the unused arguments and test for missing required inputs
 for jj=1:size(narglist,1)
     if  strcmp(paramtype{jj}, 'Toggle')
        if (narglist{jj,2} ==0)
          narglist{jj,1} = '';
        end;
        narglist{jj,2} = '';
     end;
     if  ~strcmp(narglist{jj,2},'__input') && ~strcmp(narglist{jj,2},'__output') && istoggle(jj) && ~ was_set(jj)
          narglist{jj,1} = '';
          narglist{jj,2} = '';
     end;
     if strcmp(narglist{jj,2}, '__input')
      if (minval{jj} == 0)  % meaning this input is required
        if (nextinput > size(Inputs))
           error(['Required input ' narglist{jj,1} ' has no corresponding input in the list!']);
        else
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        end
      else  % this is an optional input
        if (nextinput <= length(Inputs))
          narglist{jj,2} = 'OK_in';
          nextinput = nextinput + 1;
        else
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end;
     else
     if strcmp(narglist{jj,2}, '__output')
      if (minval{jj} == 0) % this is a required output
        if (nextoutput > nargout & nargout > 1)
           error(['Required output ' narglist{jj,1} ' is not stated in the assignment list!']);
        else
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
          NumReqOutputs = NumReqOutputs-1;
        end
      else % this is an optional output
        if (nargout - nextoutput >= NumReqOutputs)
          narglist{jj,2} = 'OK_out';
          nextoutput = nextoutput + 1;
        else
          narglist{jj,1} = '';
          narglist{jj,2} = '';
        end;
      end
     end
  end
end
if nargout
   varargout = cell(1,nargout);
else
  varargout = cell(0);
end
global KhorosRoot
if exist('KhorosRoot') && ~isempty(KhorosRoot)
w=['"' KhorosRoot];
else
if ispc
  w='"C:\Program Files\dip\khorosBin\';
else
[s,w] = system('which cantata');
w=['"' w(1:end-8)];
end
end
callKhoros([w 'xprism"  '],Inputs,narglist);


function varargout = VBA_spm_select(varargin)
% File selector
% FORMAT [t,sts] = spm_select(n,typ,mesg,sel,wd,filt,frames)
% n      - number of files [Default: Inf]
%          A single value or a range.  e.g.
%          1       - select one file
%          Inf     - select any number of files
%          [1 Inf] - select 1 to Inf files
%          [0 1]   - select 0 or 1 files
%          [10 12] - select from 10 to 12 files
% typ    - file type [Default: 'any']
%          'any'   - all files
%          'image' - Image files (".img" and ".nii")
%                    Note that it gives the option to select individuals
%                    volumes of the images.
%          'mesh'  - Surface mesh files (".gii")
%          'xml'   - XML files
%          'mat'   - MATLAB .mat files or .txt files (assumed to contain
%                    ASCII representation of a 2D-numeric array)
%          'batch' - SPM batch files (.m, .mat and XML)
%          'dir'   - select a directory
%          Other strings act as a filter to regexp. This means that
%          e.g. DCM*.mat files should have a typ of '^DCM.*\.mat$'
% mesg   - a prompt [Default: 'Select files...']
% sel    - list of already selected files [Default: {}]
% wd     - directory to start off in [Default: pwd]
% filt   - value for user-editable filter [Default: '.*']
% frames - image frame numbers to include [Default: '1']
%
% t      - selected files
% sts    - status (1 means OK, 0 means window quit)
%
% FORMAT [files,dirs] = spm_select('List',direc,filt)
% Return files matching the filter 'filt' and directories within 'direc'
% direc  - directory to search [Default: pwd]
% filt   - filter to select files with regexp, e.g. '^w.*\.img$' [Default: '.*']
%
% files  - files matching 'filt' in directory 'direc'
% dirs   - subdirectories of 'direc'
%
% FORMAT [files,dirs] = spm_select('ExtList',direc,filt,frames)
% As above, but for selecting frames of 4D NIfTI files
% frames - vector of frames to select (defaults to 1, if not specified).
%          If the frame number is Inf, all frames for the matching images
%          are listed.
%
% FORMAT [files,dirs] = spm_select('FPList',direc,filt)
% FORMAT [files,dirs] = spm_select('ExtFPList',direc,filt,frames)
% As above, but return files with full paths (i.e. prefixes 'direc' to each)
%
% FORMAT [files,dirs] = spm_select('FPListRec',direc,filt)
% FORMAT [files,dirs] = spm_select('ExtFPListRec',direc,filt,frames)
% As above, but return files with full paths (i.e. prefixes 'direc' to each)
% and search through sub directories recursively.
%__________________________________________________________________________
% Copyright (C) 2005-2014 Wellcome Trust Centre for Neuroimaging

% John Ashburner
% $Id: spm_select.m 6181 2014-09-17 16:35:39Z guillaume $

% For developers:
%--------------------------------------------------------------------------
% FORMAT cpath = spm_select('CPath',path,cwd)
% Canonicalise paths: prepend cwd to relative paths, process '..' & '.'
% directories embedded in path.
% path   - string matrix containing path name
% cwd    - current working directory [Default: '.']
% cpath  - canonicalised paths, in same format as input path argument
%
% FORMAT [t,ind] = spm_select('Filter',files,typ,filt,frames)
% Filter the list of files (cell or char array) in the same way as the
% GUI would do. There is an additional typ 'extimage' which will match
% images with frame specifications, too. Also, there is a typ 'extdir',
% which will match canonicalised directory names. The 'frames' argument
% is currently ignored, i.e. image files will not be filtered out if
% their frame numbers do not match.
% t returns the filtered list (cell or char array, depending on input),
% ind an index array, such that t = files{ind}, or t = files(ind,:).
%
% FORMAT spm_select('PrevDirs',dir)
% Add directory dir to list of previous directories.
% FORMAT dirs = spm_select('PrevDirs')
% Retrieve list of previous directories.
%
% FORMAT files = spm_select('Expand',files)
% Return a list of image filenames with appended frame numbers.

persistent isInitSelect;
if isempty(isInitSelect)
    isInitSelect = true;
    VBA_spm_select('init');
    if nargin == 1 && strcmpi(varargin{1},'init'), return; end
end

%-Commands that are not passed to cfg_getfile
local_cmds = {'regfilter', 'init', 'expand'};

if nargin && ischar(varargin{1}) && any(strcmpi(varargin{1},local_cmds))
    switch lower(varargin{1})
        case 'init'
            if ~isdeployed && ~exist('cfg_getfile','file')
                addpath(fullfile(spm('dir'),'matlabbatch'));
            end
            VBA_spm_select('regfilter');
            VBA_spm_select('prevdirs',spm('Dir'));

        case 'regfilter'
            % Regexp based filters without special handlers
            cfg_getfile('regfilter', 'mesh',  {'\.gii$'});
            cfg_getfile('regfilter', 'gifti', {'\.gii$'});
            cfg_getfile('regfilter', 'nifti', {'\.nii$','\.img$'});
            % Filter for 3D images that handles frame expansion
            frames = cfg_entry;
            frames.name    = 'Frames';
            frames.tag     = 'frames';
            frames.strtype = 'n';
            frames.num     = [1 Inf];
            frames.val     = {1};
            cfg_getfile('regfilter', 'image',...
                {'.*\.nii(,\d+){0,2}$','.*\.img(,\d+){0,2}$'},...
                false, @VBA_spm_select_image, {frames});

        case 'expand'
            varargout{1} = VBA_spm_select_expand(varargin{2});
            if ischar(varargin{2}), varargout{1} = char(varargout{1}); end
    end
else
    needchar = false;
    % cfg_getfile expects cellstr arguments for multi-line strings
    if nargin > 1 && ischar(varargin{1}) && ...
            ismember(lower(varargin{1}),{'filter','cpath'}) && ischar(varargin{2})
        varargin{2} = cellstr(varargin{2});
        needchar = true;
    elseif nargin > 0 && ischar(varargin{1}) && ...
            ismember(lower(varargin{1}),{'extlist','extfplist','extfplistrec'})
        varargin{1} = varargin{1}(4:end);
        if nargin > 3
            varargin{5} = struct('frames', varargin{4});
        else
            varargin{5} = struct('frames', Inf);
        end
        varargin{4} = varargin{3};
        varargin{3} = 'image';
    end

    [t, sts] = cfg_getfile(varargin{:});

    % cfg_getfile returns cellstr arrays, convert to char arrays
    if nargin > 0 && ischar(varargin{1})
        switch lower(varargin{1})
            case {'filter','cpath'}
                if needchar
                    t = char(t);
                end
            case {'list','fplist','extlist','extfplist','fplistrec','extfplistrec'}
                t = char(t);
                sts = char(sts);
        end
    else
        t = char(t);
    end

    varargout{1} = t;
    if nargout > 1
        varargout{2} = sts;
    end
end

%==========================================================================
% FUNCTION varargout = spm_select_image(cmd, varargin)
%==========================================================================
function varargout = VBA_spm_select_image(cmd, varargin)
% Implements extended filtering for NIfTI images (including 3D frame
% selection)
switch lower(cmd)
    case 'list'
        dr     = varargin{1};
        files  = varargin{2};
        prms   = varargin{3};
        frames = prms.frames;
        ii = cell(1,numel(files));
        if numel(frames)==1 && isnan(frames)
            [ii{:}] = deal(1);
        elseif numel(frames)~=1 || frames(1)~=1
            % if domsg
            %     msg(ob,['Reading headers of ' num2str(numel(f)) ' images...']);
            % end
            for i=1:numel(files)
                try
                    n = VBA_spm_select_get_nbframes(fullfile(dr,files{i}));
                    d4 = (1:n)';
                catch
                    d4 = 1;
                end
                if all(isfinite(frames))
                    ii{i} = intersect(d4, frames(:))';
                else
                    ii{i} = d4(:)';
                end
            end
        elseif numel(frames)==1 && frames(1)==1
            [ii{:}] = deal(1);
        end

        % if domsg
        %     msg(ob,['Listing ' num2str(numel(f)) ' files...']);
        % end

        % Combine filename and frame number(s)
        nii      = cellfun(@numel, ii);
        cfiles   = cell(sum(nii),1);
        fi       = cell(numel(files),1);
        for k = 1:numel(fi)
            fi{k} = k*ones(1,nii(k));
        end
        ii = [ii{:}];
        fi = [fi{:}]';
        if numel(frames)==1 && isnan(frames)
            cfiles = files;
        else
            for i=1:numel(cfiles)
                cfiles{i} = sprintf('%s,%d', files{fi(i)}, ii(i));
            end
        end
        varargout{1} = cfiles;
        varargout{2} = fi;
    case 'filter'
        % Do not filter for frame numbers
        varargout{1} = varargin{1};
        varargout{2} = 1:numel(varargout{1});
end

%==========================================================================
% FUNCTION ofiles = spm_select_expand(ifiles)
%==========================================================================
function ofiles = VBA_spm_select_expand(ifiles)
ifiles = cellstr(ifiles);
ofiles = {};
for i=1:numel(ifiles)
    [p,f,e,n] = VBA_spm_fileparts(ifiles{i});
    if ~isempty(n)
        ofiles = [ofiles; ifiles{i}];
    else
        n = VBA_spm_select_get_nbframes(ifiles{i});
        vfiles = cell(n,1);
        for j=1:n
            vfiles{j} = [ifiles{i} ',' num2str(j)];
        end
        ofiles = [ofiles; vfiles];
    end
end

%==========================================================================
% FUNCTION n = spm_select_get_nbframes(file)
%==========================================================================
function n = VBA_spm_select_get_nbframes(file)
N   = nifti(file);
dim = [N.dat.dim 1 1 1 1 1];
n   = dim(4);

% % A faster, direct implementation
% [p,f,e] = fileparts(file);
% unchanged = true;
% ind = find(e==',');
% if ~isempty(ind)
%     unchanged = false;
%     e = e(1:(ind(1)-1));
% end
% switch e
%     case {'.img'}
%         unchanged = false;
%         e = '.hdr';
%     case {'.IMG'}
%         unchanged = false;
%         e = '.HDR';
% end
% if ~unchanged
%     file = fullfile(p,[f e]);
% end
%
% n = [];
%
% fp  = fopen(file,'r','native');
% if fp==-1, return; end
%
% %sts = fseek(fp,344,'bof');
% %if sts==-1, fclose(fp); return; end
% %mgc = deblank(char(fread(fp,4,'uint8')'));
%
% sts = fseek(fp,40,'bof');
% if sts==-1, fclose(fp); return; end
% dim = fread(fp,8,'*int16');
% fclose(fp);
% if isempty(dim), return; end
% if dim(1)<1 || dim(1)>7
%     dim = swapbytes(dim);
% end
% dim = double(dim(2:(dim(1)+1)))';
% dim = [dim 1 1 1 1 1];
% n   = dim(4);


function nlp = Time(nlp, varargin)

    plant = nlp.Plant;
    T = SymVariable('t',[2,1]);

    tf = SymFunction(['time_' plant.Name],T(2),{T});
    addNodeCost(nlp,tf,{'T'},'first');
end


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

% Last Modified by GUIDE v2.5 20-Jan-2014 15:46:34

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

handles.Simuh=varargin{1};
% Load pulse element list
try
    handles.SeqElemListStruct=DoParseXML([handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'SeqElem.xml']);
    guidata(hObject,handles);
    Root=DoConvStruct2Tree(handles.SeqElemListStruct);
    handles.SeqElemListTree=uitree('v0','Root',Root,'SelectionChangeFcn', {@ChkSeqElemListTreeNode,handles});
    set(handles.SeqElemListTree.getUIContainer,'Units','normalized');
    set(handles.SeqElemListTree.getUIContainer,'Position',[0.0,0.2,0.125,0.8]);
    handles.Attrh0=[];
    handles.Attrh1=[];
    handles.Attrh2=[];
    handles.Attrh3=[];
    handles.Attrh4=[];
catch ME
    errordlg('SeqElem.xml file is missing or can not be loaded!');
    close(hObject);
    return;
end

handles.SeqAttri_tabgroup=uitabgroup(handles.SeqAttri_uipanel);
handles.rf_tab=uitab( handles.SeqAttri_tabgroup, 'title', 'rf');
handles.Gz_tab=uitab( handles.SeqAttri_tabgroup, 'title', 'Gz');
handles.Gy_tab=uitab( handles.SeqAttri_tabgroup, 'title', 'Gy');
handles.Gx_tab=uitab( handles.SeqAttri_tabgroup, 'title', 'Gx');

%Load tabs for Spin Property & Gradient
try
    handles.rfSimuAttrStruct=DoParseXML([handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'rfSimuAttr.xml']);
catch ME
    errordlg('rfSimuAttr.xml file is missing or can not be loaded!');
    close(hObject);
    return;
end

handles.Setting_tabgroup=uitabgroup(handles.Setting_uipanel);
guidata(handles.rfDesignPanel_figure,handles);
for i=1:length(handles.rfSimuAttrStruct.Children)
    eval(['handles.' handles.rfSimuAttrStruct.Children(i).Name '_tab=uitab( handles.Setting_tabgroup,' '''title'',' '''' handles.rfSimuAttrStruct.Children(i).Name ''',''Units'',''normalized'');']);
    eval(['DoEditValue(handles,handles.' handles.rfSimuAttrStruct.Children(i).Name '_tab,handles.rfSimuAttrStruct.Children(' num2str(i) ').Attributes,0,[0.25,0.25,0.0,0.08,0.08,0.02]);']);
    handles=guidata(handles.rfDesignPanel_figure);
end

%Create tabs for Slice Profile
handles.SliProfile_tabgroup=uitabgroup(handles.SliProfile_uipanel);
handles.MxMyMz_tab=uitab(handles.SliProfile_tabgroup,'title','Mx My Mz','Units','normalized');
handles.MxyMz_tab=uitab(handles.SliProfile_tabgroup,'title','|Mxy| Mz','Units','normalized');
handles.MP_tab=uitab(handles.SliProfile_tabgroup,'title','Mg Pe','Units','normalized');
handles.MxMyMz_axes=axes('parent', handles.MxMyMz_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);
handles.MxyMz_axes=axes('parent', handles.MxyMz_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);
handles.MP_axes=axes('parent', handles.MP_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);

%Create tabs for XYZ Gradient
handles.G_tabgroup=uitabgroup(handles.Gradient_uipanel);
handles.Gradz_tab=uitab(handles.G_tabgroup,'title','Gz (G/cm)','Units','normalized');
handles.Grady_tab=uitab(handles.G_tabgroup,'title','Gy (G/cm)','Units','normalized');
handles.Gradx_tab=uitab(handles.G_tabgroup,'title','Gx (G/cm)','Units','normalized');
handles.Gz_axes=axes('parent', handles.Gradz_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);
handles.Gy_axes=axes('parent', handles.Grady_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);
handles.Gx_axes=axes('parent', handles.Gradx_tab,'Position', [0.06 0.1 0.88 0.85],'XTick',[],'XTickLabel',[],'YTick',[],'YTickLabel',[]);

% Choose default command line output for rfDesignPanel
handles.output=hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes rfDesignPanel wait for user response (see UIRESUME)
% uiwait(handles.rfDesignPanel_figure);

function ChkSeqElemListTreeNode(tree, value, handles)

handles=guidata(handles.rfDesignPanel_figure);
SelNode=tree.SelectedNodes;
SelNode=SelNode(1);
Level=SelNode.getValue;
if ~ischar(Level)
    LevelChg=diff(Level,1);
    Level([LevelChg; -1]==1)=[];
end

Node='handles.SeqElemListStruct';
if Level=='0'
    eval(['handles.SeqElemNode=' Node ';']);
else
    for i=1:length(Level)
        Node=[Node '.Children(' num2str(Level(i)) ')'];
    end
    eval(['handles.SeqElemNode=' Node ';']);
end

set(handles.SeqAttri_uipanel,'Title',['Checking ''' char(SelNode.getName) '''']);
if SelNode.isLeaf | SelNode.isRoot

    if strcmp(handles.SeqElemNode.Name(1:2),'rf') & length(handles.SeqElemNode.Name)>2

        if ~isempty(handles.Attrh1)
            Attrh1name=fieldnames(handles.Attrh1);
            for i=1:length(Attrh1name)
                delete(handles.Attrh1.(Attrh1name{i}));
            end
            handles.Attrh1=[];
        end
        handles.rfNode=handles.SeqElemNode;
        handles.rfNodeLvl=Node;
        set(handles.rf_tab,'Title',['rf:' handles.SeqElemNode.Name]);
        DoEditValue(handles,handles.rf_tab,handles.SeqElemNode.Attributes,1,[0.50,0.50,0.0,0.05,0.05,0.005]);

        %----load rf memo
        fid=fopen([handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'rf' filesep handles.SeqElemNode.Name '_Memo.txt'],'r');
        if fid==-1
            set(handles.rfMemo_edit,'String','rf Memo is not available!');
        else
            tline = fgetl(fid);
            i=1;
            while ischar(tline)
                Memo{i,1}=tline;
                tline = fgetl(fid);
                i=i+1;
            end
            if i==1
                set(handles.rfMemo_edit,'String','rf Memo is empty!');
            else
                set(handles.rfMemo_edit,'String',Memo);
            end
            fclose(fid);
        end
        %----end
        set(handles.UpdateAttrXML_pushbutton,'Enable','on');
     elseif strcmp(handles.SeqElemNode.Name(1:2),'Gz') & length(handles.SeqElemNode.Name)>4

        if ~isempty(handles.Attrh2)
            Attrh2name=fieldnames(handles.Attrh2);
            for i=1:length(Attrh2name)
                delete(handles.Attrh2.(Attrh2name{i}));
            end
            handles.Attrh2=[];
        end
        handles.GzNode=handles.SeqElemNode;
        handles.GzNodeLvl=Node;
        set(handles.Gz_tab,'Title',['Gz:' handles.SeqElemNode.Name]);
        DoEditValue(handles,handles.Gz_tab,handles.SeqElemNode.Attributes,2,[0.50,0.50,0.0,0.05,0.05,0.015]);
        set(handles.UpdateAttrXML_pushbutton,'Enable','on');
     elseif strcmp(handles.SeqElemNode.Name(1:2),'Gy') & length(handles.SeqElemNode.Name)>4

        if ~isempty(handles.Attrh3)
            Attrh3name=fieldnames(handles.Attrh3);
            for i=1:length(Attrh3name)
                delete(handles.Attrh3.(Attrh3name{i}));
            end
            handles.Attrh3=[];
        end
        handles.GyNode=handles.SeqElemNode;
        handles.GyNodeLvl=Node;
        set(handles.Gy_tab,'Title',['Gy:' handles.SeqElemNode.Name]);
        DoEditValue(handles,handles.Gy_tab,handles.SeqElemNode.Attributes,3,[0.50,0.50,0.0,0.05,0.05,0.015]);
        set(handles.UpdateAttrXML_pushbutton,'Enable','on');
     elseif strcmp(handles.SeqElemNode.Name(1:2),'Gx') & length(handles.SeqElemNode.Name)>4

        if ~isempty(handles.Attrh4)
            Attrh4name=fieldnames(handles.Attrh4);
            for i=1:length(Attrh4name)
                delete(handles.Attrh4.(Attrh4name{i}));
            end
            handles.Attrh4=[];
        end
        handles.GxNode=handles.SeqElemNode;
        handles.GxNodeLvl=Node;
        set(handles.Gx_tab,'Title',['Gx:' handles.SeqElemNode.Name]);
        DoEditValue(handles,handles.Gx_tab,handles.SeqElemNode.Attributes,4,[0.50,0.50,0.0,0.05,0.05,0.015]);
        set(handles.UpdateAttrXML_pushbutton,'Enable','on');
    end
else
    guidata(handles.rfDesignPanel_figure, handles);
end

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

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

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

% set(handles.rf_line,'XData',[get(hObject,'Value'),get(hObject,'Value')]','YData',[min(handles.rfAmp) max(handles.rfAmp)]');

rf_step=round(get(hObject,'Value'));
[az,el] = view(handles.SpinRot_axes);

if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')

    Mx=handles.Mx(:,:,:,:,:,rf_step);
    My=handles.My(:,:,:,:,:,rf_step);
    Mz=handles.Mz(:,:,:,:,:,rf_step);
    Mxy=sqrt(Mx.^2+My.^2);
    Pe=angle(Mx+1i.*My);
    for j=1:str2double(get(handles.Attrh0.TypeNum,'String'))
        spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,j),My(:,:,:,:,j),Mz(:,:,:,:,j));
        set(spinver,'AutoScale','off');
        hold(handles.SpinRot_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxMyMz_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,j)),'Color',[0 1-(j-1)*0.25 0],'linewidth',2);
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxyMz_axes,'on');
        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MP_axes,'on');
        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);
    end
    hold(handles.SpinRot_axes,'off');
    hold(handles.MxMyMz_axes,'off');
    hold(handles.MxyMz_axes,'off');
    hold(handles.MP_axes,'off');

    set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
    xlabel(handles.SpinRot_axes,'X axis');
    ylabel(handles.SpinRot_axes,'Y axis');
    zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
    set(handles.SpinRot_axes,'view',[az,el]);

    set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxMyMz_axes,'Mx','My','Mz');

    set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxyMz_axes,'|Mxy|','Mz');

    set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MP_axes,'YGrid','on','XGrid','on');
    legend(handles.MP_axes,'Mg','Pe');

else
    updateSS(handles, rf_step);
end

plot(handles.rf_axes,handles.rfTime,handles.rfAmp,...
    [handles.Muts(rf_step) handles.Muts(rf_step)],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
set(handles.rf_axes,'YGrid','on','XGrid','on');
%set(handles.SpinRot_axes,'ZTickLabel',get(handles.SpinRot_axes,'ZTick')*10);
%set(handles.MxMyMz_axes,'XTickLabel',get(handles.MxMyMz_axes,'XTick')*100);
set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

% --- Executes during object creation, after setting all properties.
function rf_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rf_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 on button press in Right_pushbutton.
function Right_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Right_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[az,el] = view(handles.SpinRot_axes);
Starti=round(get(handles.rf_slider,'Value'));
for i=Starti+1:max(size(handles.Muts))
    handles=guidata(hObject);
    if handles.PauseFlag==1
        return;
    end
    set(handles.rf_slider,'Value',i);
    rf_step=round(get(handles.rf_slider,'Value'));

    if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')

        Mx=handles.Mx(:,:,:,:,:,rf_step);
        My=handles.My(:,:,:,:,:,rf_step);
        Mz=handles.Mz(:,:,:,:,:,rf_step);
        Mxy=sqrt(Mx.^2+My.^2);
        Pe=angle(Mx+1i.*My);
        for j=1:str2double(get(handles.Attrh0.TypeNum,'String'))
            spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,j),My(:,:,:,:,j),Mz(:,:,:,:,j));
            set(spinver,'AutoScale','off');
            hold(handles.SpinRot_axes,'on');
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MxMyMz_axes,'on');
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,j)),'Color',[0 1-(j-1)*0.25 0],'linewidth',2);
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

            plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MxyMz_axes,'on');
            plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

            plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MP_axes,'on');
            plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);
        end
        hold(handles.SpinRot_axes,'off');
        hold(handles.MxMyMz_axes,'off');
        hold(handles.MxyMz_axes,'off');
        hold(handles.MP_axes,'off');

        set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
        xlabel(handles.SpinRot_axes,'X axis');
        ylabel(handles.SpinRot_axes,'Y axis');
        zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
        set(handles.SpinRot_axes,'view',[az,el]);

        set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');
        legend(handles.MxMyMz_axes,'Mx','My','Mz');

        set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
        legend(handles.MxyMz_axes,'|Mxy|','Mz');

        set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MP_axes,'YGrid','on','XGrid','on');
        legend(handles.MP_axes,'Mg','Pe');

    else
        updateSS(handles, rf_step);
    end

    plot(handles.rf_axes,handles.rfTime,handles.rfAmp,...
        [handles.Muts(rf_step) handles.Muts(rf_step)],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
    set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
    set(handles.rf_axes,'YGrid','on','XGrid','on');
    %set(handles.SpinRot_axes,'ZTickLabel',get(handles.SpinRot_axes,'ZTick')*10);
    %set(handles.MxMyMz_axes,'XTickLabel',get(handles.MxMyMz_axes,'XTick')*100);
    set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
    set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
    set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

    pause(0.1);
end

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

[az,el] = view(handles.SpinRot_axes);
Starti=round(get(handles.rf_slider,'Value'));
for i=Starti+1:max(size(handles.Muts))
    handles=guidata(hObject);
    if handles.PauseFlag==1
        return;
    end
    set(handles.rf_slider,'Value',i);
    rf_step=round(get(handles.rf_slider,'Value'));

    if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')

        Mx=handles.Mx(:,:,:,:,:,rf_step);
        My=handles.My(:,:,:,:,:,rf_step);
        Mz=handles.Mz(:,:,:,:,:,rf_step);
        Mxy=sqrt(Mx.^2+My.^2);
        Pe=angle(Mx+1i.*My);
        for j=1:str2double(get(handles.Attrh0.TypeNum,'String'))
            spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,j),My(:,:,:,:,j),Mz(:,:,:,:,j));
            set(spinver,'AutoScale','off');
            hold(handles.SpinRot_axes,'on');
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MxMyMz_axes,'on');
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,j)),'Color',[0 1-(j-1)*0.25 0],'linewidth',2);
            plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

            plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MxyMz_axes,'on');
            plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

            plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
            hold(handles.MP_axes,'on');
            plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);
        end
        hold(handles.SpinRot_axes,'off');
        hold(handles.MxMyMz_axes,'off');
        hold(handles.MxyMz_axes,'off');
        hold(handles.MP_axes,'off');

        set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
        xlabel(handles.SpinRot_axes,'X axis');
        ylabel(handles.SpinRot_axes,'Y axis');
        zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
        set(handles.SpinRot_axes,'view',[az,el]);

        set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');
        legend(handles.MxMyMz_axes,'Mx','My','Mz');

        set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
        legend(handles.MxyMz_axes,'|Mxy|','Mz');

        set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
        set(handles.MP_axes,'YGrid','on','XGrid','on');
        legend(handles.MP_axes,'Mg','Pe');

    else
        updateSS(handles, rf_step);
    end

    plot(handles.rf_axes,handles.rfTime,handles.rfAmp,...
        [handles.Muts(rf_step) handles.Muts(rf_step)],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
    set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
    set(handles.rf_axes,'YGrid','on','XGrid','on');
    %set(handles.SpinRot_axes,'ZTickLabel',get(handles.SpinRot_axes,'ZTick')*10);
    %set(handles.MxMyMz_axes,'XTickLabel',get(handles.MxMyMz_axes,'XTick')*100);
    set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
    set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
    set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

    pause(0.001);
end

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

set(handles.rf_slider,'Value',1);
rf_step=round(get(handles.rf_slider,'Value'));
[az,el] = view(handles.SpinRot_axes);

if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')

    Mx=handles.Mx(:,:,:,:,:,rf_step);
    My=handles.My(:,:,:,:,:,rf_step);
    Mz=handles.Mz(:,:,:,:,:,rf_step);
    Mxy=sqrt(Mx.^2+My.^2);
    Pe=angle(Mx+1i.*My);
    for j=1:str2double(get(handles.Attrh0.TypeNum,'String'))
        spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,j),My(:,:,:,:,j),Mz(:,:,:,:,j));
        set(spinver,'AutoScale','off');
        hold(handles.SpinRot_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxMyMz_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,j)),'Color',[0 1-(j-1)*0.25 0],'linewidth',2);
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxyMz_axes,'on');
        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MP_axes,'on');
        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);
    end
    hold(handles.SpinRot_axes,'off');
    hold(handles.MxMyMz_axes,'off');
    hold(handles.MxyMz_axes,'off');
    hold(handles.MP_axes,'off');

    set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
    xlabel(handles.SpinRot_axes,'X axis');
    ylabel(handles.SpinRot_axes,'Y axis');
    zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
    set(handles.SpinRot_axes,'view',[az,el]);

    set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxMyMz_axes,'Mx','My','Mz');

    set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxyMz_axes,'|Mxy|','Mz');

    set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MP_axes,'YGrid','on','XGrid','on');
    legend(handles.MP_axes,'Mg','Pe');

else
    updateSS(handles, rf_step);
end

plot(handles.rf_axes,handles.rfTime,handles.rfAmp,...
    [handles.Muts(rf_step) handles.Muts(rf_step)],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
set(handles.rf_axes,'YGrid','on','XGrid','on');
%set(handles.SpinRot_axes,'ZTickLabel',get(handles.SpinRot_axes,'ZTick')*10);
%set(handles.MxMyMz_axes,'XTickLabel',get(handles.MxMyMz_axes,'XTick')*100);
set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

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

set(handles.rf_slider,'Value',max(size(handles.Muts)));
rf_step=round(get(handles.rf_slider,'Value'));
[az,el] = view(handles.SpinRot_axes);

if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')

    Mx=handles.Mx(:,:,:,:,:,rf_step);
    My=handles.My(:,:,:,:,:,rf_step);
    Mz=handles.Mz(:,:,:,:,:,rf_step);
    Mxy=sqrt(Mx.^2+My.^2);
    Pe=angle(Mx+1i.*My);
    for j=1:str2double(get(handles.Attrh0.TypeNum,'String'))
        spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,j),My(:,:,:,:,j),Mz(:,:,:,:,j));
        set(spinver,'AutoScale','off');
        hold(handles.SpinRot_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxMyMz_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,j)),'Color',[0 1-(j-1)*0.25 0],'linewidth',2);
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MxyMz_axes,'on');
        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);

        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,j)),'Color',[0 0 1-(j-1)*0.25],'linewidth',2);
        hold(handles.MP_axes,'on');
        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,j)),'Color',[1-(j-1)*0.25 0 0],'linewidth',2);
    end
    hold(handles.SpinRot_axes,'off');
    hold(handles.MxMyMz_axes,'off');
    hold(handles.MxyMz_axes,'off');
    hold(handles.MP_axes,'off');

    set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
    xlabel(handles.SpinRot_axes,'X axis');
    ylabel(handles.SpinRot_axes,'Y axis');
    zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
    set(handles.SpinRot_axes,'view',[az,el]);

    set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxMyMz_axes,'Mx','My','Mz');

    set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxyMz_axes,'|Mxy|','Mz');

    set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MP_axes,'YGrid','on','XGrid','on');
    legend(handles.MP_axes,'Mg','Pe');

else
    updateSS(handles, rf_step);
end

plot(handles.rf_axes,handles.rfTime,handles.rfAmp,...
    [handles.Muts(rf_step) handles.Muts(rf_step)],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
set(handles.rf_axes,'YGrid','on','XGrid','on');
set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

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

set(hObject,'Enable','off');
set(hObject,'String','Calc...');
pause(0.01);
ExecFlag=DorfExec(handles);
set(handles.Simuh.TimeWait_text,'String', ['Est. Time Left :  ' '~' ' : ' '~' ' : ' '~']);
handles=guidata(handles.rfDesignPanel_figure);
if ExecFlag==0
    set(hObject,'Enable','on');
    set(hObject,'String','Execute');
    return;
end
handles.PauseFlag=0;
set(handles.Leftend_pushbutton,'Enable','on');
set(handles.Rightend_pushbutton,'Enable','on');
set(handles.Pause_pushbutton,'Enable','on');
set(handles.Right_pushbutton,'Enable','on');
set(handles.DoubleRight_pushbutton,'Enable','on');
set(handles.rf_slider,'Enable','on');
%% Display
% rf Response & Spin Rotation
if strcmp(handles.Freq_Flag,'off') & strcmp(handles.Spat_Flag,'off')
    if ~isfield(handles,'MxMyMz_tab')
        %----clear tabs
        tabs=get(handles.SliProfile_tabgroup,'Children');
        for i=1:length(tabs)
            delete(get(tabs(i),'Children'));
        end
        delete(tabs);
        delete(handles.SliProfile_tabgroup);
        handles=rmfield(handles,'Mx_tab');
        %----end
        %----recreate tabs
        handles.SliProfile_tabgroup=uitabgroup(handles.SliProfile_uipanel);
        handles.MxMyMz_tab=uitab(handles.SliProfile_tabgroup,'title','Mx My Mz','Units','normalized');
        handles.MxyMz_tab=uitab(handles.SliProfile_tabgroup,'title','|Mxy| Mz','Units','normalized');
        handles.MP_tab=uitab(handles.SliProfile_tabgroup,'title','Mg Pe','Units','normalized');
        handles.MxMyMz_axes=axes('parent', handles.MxMyMz_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.MxyMz_axes=axes('parent', handles.MxyMz_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.MP_axes=axes('parent', handles.MP_tab,'Position', [0.06 0.1 0.88 0.85]);
        %----end
    end
    Mx=handles.Mx(:,:,:,:,:,end);
    My=handles.My(:,:,:,:,:,end);
    Mz=handles.Mz(:,:,:,:,:,end);
    Mxy=sqrt(Mx.^2+My.^2);
    Pe=angle(Mx+1i.*My);
    for i=1:str2double(get(handles.Attrh0.TypeNum,'String'))
        spinver=quiver3(handles.SpinRot_axes,handles.Gxgrid,handles.Gygrid,handles.Gzgrid,Mx(:,:,:,:,i),My(:,:,:,:,i),Mz(:,:,:,:,i));
        set(spinver,'AutoScale','off');
        hold(handles.SpinRot_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mx(:,:,:,:,i)),'Color',[0 0 1-(i-1)*0.25],'linewidth',2);
        hold(handles.MxMyMz_axes,'on');
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(My(:,:,:,:,i)),'Color',[0 1-(i-1)*0.25 0],'linewidth',2);
        plot(handles.MxMyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,i)),'Color',[1-(i-1)*0.25 0 0],'linewidth',2);

        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,i)),'Color',[0 0 1-(i-1)*0.25],'linewidth',2);
        hold(handles.MxyMz_axes,'on');
        plot(handles.MxyMz_axes,squeeze(handles.Gzgrid),squeeze(Mz(:,:,:,:,i)),'Color',[1-(i-1)*0.25 0 0],'linewidth',2);

        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Mxy(:,:,:,:,i)),'Color',[0 0 1-(i-1)*0.25],'linewidth',2);
        hold(handles.MP_axes,'on');
        plot(handles.MP_axes,squeeze(handles.Gzgrid),squeeze(Pe(:,:,:,:,i)),'Color',[1-(i-1)*0.25 0 0],'linewidth',2);
    end
    hold(handles.SpinRot_axes,'off');
    hold(handles.MxMyMz_axes,'off');
    hold(handles.MxyMz_axes,'off');
    hold(handles.MP_axes,'off');
    set(handles.SpinRot_axes,'YLim',[-1 1],'XLim',[-1 1],'ZLim',[min(handles.Gzgrid,[],3)-1 max(handles.Gzgrid,[],3)+1]);
    xlabel(handles.SpinRot_axes,'X axis');
    ylabel(handles.SpinRot_axes,'Y axis');
    zlabel(handles.SpinRot_axes,'Z axis (Gradient Direction)');
    set(handles.SpinRot_axes,'view',[134,24]);

    set(handles.MxMyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    legend(handles.MxMyMz_axes,'Mx','My','Mz');
    set(handles.MxMyMz_axes,'YGrid','on','XGrid','on');

    set(handles.MxyMz_axes,'YLim',[-1 1],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MxyMz_axes,'YGrid','on','XGrid','on');
    legend(handles.MxyMz_axes,'|Mxy|','Mz');

    set(handles.MP_axes,'YLim',[-pi pi],'XLim',[min(handles.Gzgrid,[],3) max(handles.Gzgrid,[],3)]);
    set(handles.MP_axes,'YGrid','on','XGrid','on');
    legend(handles.MP_axes,'Mg','Pe');
else

    if ~isfield(handles,'Mx_tab')
        %----clear tabs
        tabs=get(handles.SliProfile_tabgroup,'Children');
        for i=1:length(tabs)
            delete(get(tabs(i),'Children'));
        end
        delete(tabs);
        delete(handles.SliProfile_tabgroup);
        handles=rmfield(handles,'MxMyMz_tab');
        %----end
        %----recreate tab
        handles.SliProfile_tabgroup=uitabgroup(handles.SliProfile_uipanel);
        handles.Mx_tab=uitab(handles.SliProfile_tabgroup,'title','Mx','Units','normalized');
        handles.My_tab=uitab(handles.SliProfile_tabgroup,'title','My','Units','normalized');
        handles.Mz_tab=uitab(handles.SliProfile_tabgroup,'title','Mz','Units','normalized');
        handles.M_tab=uitab(handles.SliProfile_tabgroup,'title','Mag','Units','normalized');
        handles.P_tab=uitab(handles.SliProfile_tabgroup,'title','Ph','Units','normalized');

        handles.Mx_axes=axes('parent', handles.Mx_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.My_axes=axes('parent', handles.My_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.Mz_axes=axes('parent', handles.Mz_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.M_axes=axes('parent', handles.M_tab,'Position', [0.06 0.1 0.88 0.85]);
        handles.P_axes=axes('parent', handles.P_tab,'Position', [0.06 0.1 0.88 0.85]);
        %----end
    end

    updateSS(handles, size(handles.Mx, 3));
    set(handles.SpinRot_axes,'view',[134,24]);
end

set(handles.rf_slider,'Min',1);
set(handles.rf_slider,'Max',max(size(handles.Muts)));
set(handles.rf_slider,'Value',max(size(handles.Muts)));

plot(handles.rf_axes,handles.rfTime,handles.rfAmp,[handles.Muts(get(handles.rf_slider,'Value')) handles.Muts(get(handles.rf_slider,'Value'))],[min(handles.rfAmp) max(handles.rfAmp)],'g-','linewidth',2);
plot(handles.rfPhase_axes,handles.rfTime,handles.rfPhase,'linewidth',2);
plot(handles.rfFreq_axes,handles.rfTime,handles.rfFreq,'linewidth',2);

if strcmp(handles.Spat_Flag,'off')
    plot(handles.Gz_axes,handles.GzTime,handles.GzAmp,'linewidth',2);
    set(handles.Gz_axes,'YGrid','on','XGrid','off','XTick',[]);
    set(handles.Gz_axes,'YTickLabel',get(handles.Gz_axes,'YTick')*100);
    linkaxes([handles.rf_axes handles.rfPhase_axes handles.rfFreq_axes handles.Gz_axes],'x');
else
    plot(handles.Gy_axes,handles.GyTime,handles.GyAmp,'linewidth',2);
    set(handles.Gy_axes,'YGrid','on','XGrid','off','XTick',[]);
    set(handles.Gy_axes,'YTickLabel',get(handles.Gy_axes,'YTick')*100);
    plot(handles.Gx_axes,handles.GxTime,handles.GxAmp,'linewidth',2);
    set(handles.Gx_axes,'YGrid','on','XGrid','off','XTick',[]);
    set(handles.Gx_axes,'YTickLabel',get(handles.Gx_axes,'YTick')*100);
    linkaxes([handles.rf_axes handles.rfPhase_axes handles.rfFreq_axes handles.Gy_axes handles.Gx_axes ],'x');
end

set(handles.rf_axes,'YLim',[min(handles.rfAmp) max(handles.rfAmp)],'XLim',[min(handles.rfTime) max(handles.rfTime)]);
set(handles.rf_axes,'YGrid','on','XGrid','on');
set(handles.rfPhase_axes,'YGrid','on','XGrid','off','XTick',[]);
set(handles.rfFreq_axes,'YGrid','on','XGrid','off','XTick',[]);

set(handles.rf_axes,'XTickLabel',get(handles.rf_axes,'XTick')*1000);
set(handles.rf_axes,'YTickLabel',get(handles.rf_axes,'YTick')*10000);
set(handles.rfFreq_axes,'YTickLabel',get(handles.rfFreq_axes,'YTick')/1000);

set(hObject,'Enable','on');
set(hObject,'String','Execute');
guidata(handles.rfDesignPanel_figure, handles);

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

% update SeqElem.xml
if verLessThan('matlab','8.5')
    % Code to run in MATLAB R2015a and earlier here
    idx = get(handles.SeqAttri_tabgroup,'SelectedIndex');
else
    % Code to run in MATLAB R2015a and later here
    tabtitle=get(get(handles.SeqAttri_tabgroup,'SelectedTab'),'Title');
    if tabtitle(1:2) == 'rf'
        idx = 1;
    elseif tabtitle(1:2) == 'Gz'
        idx = 2;
    end
end

if idx==1 % update rf
    if ~isempty(handles.Attrh1)
        Attrh1name=fieldnames(handles.Attrh1);
        for i=1:2:length(Attrh1name)

            if ~iscell(get(handles.Attrh1.(Attrh1name{i+1}),'String'))
                handles.rfNode.Attributes((i+1)/2).Value=get(handles.Attrh1.(Attrh1name{i+1}),'String');
            else
                handles.rfNode.Attributes((i+1)/2).Value(2)=num2str(get(handles.Attrh1.(Attrh1name{i+1}),'Value'));
            end
        end
        eval([handles.rfNodeLvl '=handles.rfNode;']);
        DoWriteXML(handles.SeqElemListStruct,[handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'SeqElem.xml']);
    end

    fid=fopen([handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'rf' filesep handles.SeqElemNode.Name '_Memo.txt'],'wt+');
    Memo=get(handles.rfMemo_edit,'String');
    if ~isempty(Memo)
        if ischar(Memo)
            Memo=cellstr(Memo);
        end
        for i=1:length(Memo)
            fprintf(fid,'%s\n', Memo{i,1});
        end
    end
    fclose(fid);

elseif idx==2 % update GzSS
    if ~isempty(handles.Attrh2)
        Attrh2name=fieldnames(handles.Attrh2);
        for i=1:2:length(Attrh2name)

            if ~iscell(get(handles.Attrh2.(Attrh2name{i+1}),'String'))
                handles.GzNode.Attributes((i+1)/2).Value=get(handles.Attrh2.(Attrh2name{i+1}),'String');
            else
                handles.GzNode.Attributes((i+1)/2).Value(2)=num2str(get(handles.Attrh2.(Attrh2name{i+1}),'Value'));
            end
        end
        eval([handles.GzNodeLvl '=handles.GzNode;']);
        DoWriteXML(handles.SeqElemListStruct,[handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'SeqElem.xml']);
    end
end

% update rfSimuAttr.xml
tabs=get(handles.Setting_tabgroup,'Children');
for j=1:length(handles.rfSimuAttrStruct.Children)
    Attrh0=get(tabs(j),'Children');
    for i=1:2:length(Attrh0)
        if ~iscell(get(Attrh0(end-i),'String'))
            handles.rfSimuAttrStruct.Children(j).Attributes((i+1)/2).Value=get(Attrh0(end-i),'String');
        else
            handles.rfSimuAttrStruct.Children(j).Attributes((i+1)/2).Value(2)=num2str(get(Attrh0(end-i),'Value'));
        end
    end
end
DoWriteXML(handles.rfSimuAttrStruct,[handles.Simuh.MRiLabPath filesep 'Macro' filesep 'SeqElem' filesep 'rfSimuAttr.xml']);

guidata(hObject, handles);

% --------------------------------------------------------------------
function SavePanel_uipushtool_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to SavePanel_uipushtool (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
DoSaveSnapshot(handles.rfDesignPanel_figure);

% --- Executes on button press in Pause_pushbutton.
function Pause_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to Pause_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if handles.PauseFlag==1
    handles.PauseFlag=0;
    set(hObject,'String','X');
else
    handles.PauseFlag=1;
    set(hObject,'String','O');
end

guidata(hObject, handles);

% --- Executes when user attempts to close rfDesignPanel_figure.
function rfDesignPanel_figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to rfDesignPanel_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure

if isfield(handles,'PauseFlag')
    if handles.PauseFlag==1
        delete(hObject);
    else
        warndlg('Please pause rf animation before closing the window (i.e. press X).');
    end
else
    delete(hObject);
end

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

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

% update spin response for spatial-spectral analysis
function updateSS(handles, rf_step)

Mx=handles.Mx(:,:,rf_step);
My=handles.My(:,:,rf_step);
Mz=handles.Mz(:,:,rf_step);
Mag=sqrt(Mx.^2+My.^2);
Ph=angle(Mx+1i.*My);

axes(handles.Mx_axes);
imagesc(Mx);
colormap gray
colorbar
axes(handles.My_axes);
imagesc(My);
colorbar
axes(handles.Mz_axes);
imagesc(Mz);
colorbar
axes(handles.M_axes);
imagesc(Mag);
colorbar
axes(handles.P_axes);
imagesc(Ph);
colorbar

axes(handles.SpinRot_axes);
[az,el] = view(handles.SpinRot_axes);
cla(handles.SpinRot_axes);

if verLessThan('matlab','8.5')
    % Code to run in MATLAB R2015a and earlier here
    idx = get(handles.SliProfile_tabgroup,'SelectedIndex');
    switch idx
        case 1 % Mx
            surf(double(Mx));
            title('Mx');
        case 2 % My
            surf(double(My));
            title('My');
        case 3 % Mz
            surf(double(Mz));
            title('Mz');
        case 4 % Mag
            surf(double(Mag));
            title('Mag');
        case 5 % Ph
            surf(double(Ph));
            title('Ph');
    end
else
    % Code to run in MATLAB R2015a and later here
    tabtitle=get(get(handles.SliProfile_tabgroup,'SelectedTab'),'Title');
    switch tabtitle
        case 'Mx' % Mx
            surf(double(Mx));
            title('Mx');
        case 'My' % My
            surf(double(My));
            title('My');
        case 'Mz' % Mz
            surf(double(Mz));
            title('Mz');
        case 'Mag' % Mag
            surf(double(Mag));
            title('Mag');
        case 'Ph' % Ph
            surf(double(Ph));
            title('Ph');
    end
end

if strcmp(handles.Freq_Flag,'on')
    xlabel('Frequency');
    ylabel('Distance');
else
    xlabel('X-Distance');
    ylabel('Y-Distance');
end
shading interp;
set(handles.SpinRot_axes,'view',[az,el],'xdir','reverse');

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function rfDesignPanel_figure_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to rfDesignPanel_figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

tempaxes = gca;
try
    if strcmp(get(gcf,'Selectiontype'),'alt')
        figure 			% Create a new figure
        ax_new = axes;
        copyaxes(tempaxes, ax_new);
    end
catch me
end