You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: atlas2region.m
    path of file: ./repos/CanlabCore/CanlabCore/@atlas
    
    the code of the file until where you have to start completion: function r = atlas2region(atlas_obj, varargin)
% Convert an atlas object to a region object
% r = atlas2region(atlas_obj)
%
% Note: There are multiple potential ways this can work.
% The default is to use uses the atlas_obj.dat field, 
% which has one label per voxel. For some atlases, a region may have no
% voxels with the max probability label. 
% 
% 'use_probabilities'  uses atlas_obj.probability maps if available, 
% which guarantees at least one region per atlas region. If atlas.probability_maps is
% empty, however, the function defaults to using index labels.
% Note: this can be slow for large atlases.
%
% Another default is to parse regions with each label into contiguous
% regions. This can result in multiple contiguous regions in the region object (r)
% for a single atlas region. (e.g., those in left and right hemispheres, if
% they are given the same label in the atlas).
%
% 'nocontiguous' (or 'unique_mask_values') is an option that suppresses the 
% parsing, creating one region per label/atlas region.
% If atlas.probability_maps is empty or 'use_labels' is specified,
% the function defaults to 'nocontiguous'.
%
% Examples:
% r = atlas2region(atlas_obj);                       % Use indices in dat.dat, one region per atlas region.
% r = atlas2region(atlas_obj, 'use_probabilities');  % Use probability_maps if available  
% r = atlas2region(atlas_obj, 'nocontiguous');       % One region per atlas region
%

    
function r = atlas2region(atlas_obj, varargin)
% Convert an atlas object to a region object
% r = atlas2region(atlas_obj)
%
% Note: There are multiple potential ways this can work.
% The default is to use uses the atlas_obj.dat field, 
% which has one label per voxel. For some atlases, a region may have no
% voxels with the max probability label. 
% 
% 'use_probabilities'  uses atlas_obj.probability maps if available, 
% which guarantees at least one region per atlas region. If atlas.probability_maps is
% empty, however, the function defaults to using index labels.
% Note: this can be slow for large atlases.
%
% Another default is to parse regions with each label into contiguous
% regions. This can result in multiple contiguous regions in the region object (r)
% for a single atlas region. (e.g., those in left and right hemispheres, if
% they are given the same label in the atlas).
%
% 'nocontiguous' (or 'unique_mask_values') is an option that suppresses the 
% parsing, creating one region per label/atlas region.
% If atlas.probability_maps is empty or 'use_labels' is specified,
% the function defaults to 'nocontiguous'.
%
% Examples:
% r = atlas2region(atlas_obj);                       % Use indices in dat.dat, one region per atlas region.
% r = atlas2region(atlas_obj, 'use_probabilities');  % Use probability_maps if available  
% r = atlas2region(atlas_obj, 'nocontiguous');       % One region per atlas region
%
% Tor Wager, Jan 2018

% Programmers' notes:
% 9/24/21: region labels were wrong in some cases, if atlas object has been
    % altered to remove some regions. Tor fixed this to use region.val
    % mode.
    
% Defaults and inputs
% ----------------------------------------------------------------------------

use_probabilities_if_available = false;
region_parse_method = 'contiguous_regions';
ivec = [];

for i = 1:length(varargin)
    
    if ischar(varargin{i})
        switch varargin{i}
            
            case 'use_probabilities', use_probabilities_if_available = false;
            case {'nocontiguous', 'unique_mask_values'}
                region_parse_method = 'unique_mask_values';
                
            case 'noverbose'
                % do nothing; avoid warnings when calling from other
                % functions and passing args through
                
            otherwise , warning(['Unknown input string option:' varargin{i}]);
        end