Skip to content

Commit

Permalink
#1689 xASL_filesparts: proper management SPMsuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
HenkMutsaerts committed Apr 21, 2024
1 parent d9e3898 commit 1ee59b7
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions External/SPMmodified/xASL/xASL_fileparts.m
Original file line number Diff line number Diff line change
@@ -1,63 +1,69 @@
function [Fpath, Ffile, Fext] = xASL_fileparts(InputPath)
% Wrapper around the fileparts.m that treats nii.gz as an extension
% (this also works with other extensions)
function [Fpath, Ffile, Fext, SuffixSPM] = xASL_fileparts(InputPath)
%xASL_fileparts Wrapper around fileparts for special extensions
% FORMAT: [Fpath, Ffile, Fext] = xASL_fileparts(InputPath)
%
% INPUT:
% InputPath - input file name with path, ending either .ext or .ext.gz
% InputPath - input file name with path, ending either .ext(.gz) or .ext(.gz),n
% OUTPUT:
% Fpath - path or empty string
% Ffile - file name
% Fext - file extension or empty string
% SuffixSPM - volume extension for NIfTI files (per spm usage)
% -----------------------------------------------------------------------------------------------------------------------------------------------------
% DESCRIPTION: Returns the path, file name, and file extension for InputPath using the fileparts.m function.
% If a file ending at nii.gz is given, then the whole nii.gz is returned as the extension.
% Does not verify the existence of the file, or existence of .nii or .nii.gz
% EXAMPLE: xASL_fileparts('/path/file.nii');
% xASL_fileparts('/path/file.nii.gz','file');
% xASL_fileparts('c:\path\file.nii');
% Does not verify the existence of the file, or existence of .nii or .nii.gz.
% This function uses the following steps:
% 1. Catch SPM suffixes
% 2. Manage .gz (double) extensions
% 3. Manage folders differently than files
% -----------------------------------------------------------------------------------------------------------------------------------------------------
%
% EXAMPLES: ['/path' 'file' '.nii'] = xASL_fileparts('/path/file.nii');
% ['/path' 'file' '.nii.gz'] = xASL_fileparts('/path/file.nii.gz');
% ['/path' 'file' '.nii.gz' ',2'] = xASL_fileparts('/path/file.nii.gz,2');
% ['c:\path' 'file' '.nii'] = xASL_fileparts('c:\path\file.nii') (on Windows)
% __________________________________
% Copyright (R) 2015-2019 ExploreASL
%
% 2015-01-01 HJ
% Copyright 2015-2024 ExploreASL

%% Admin
% First catch cell
if iscell(InputPath)
if length(InputPath)>1
warning('InputPath contained multiple cells, using the first only');
end
InputPath = InputPath{1};
end

% Now catch SPM suffixes
[Ind1, Ind2] = regexp(InputPath,',\d');

%% 1. Catch SPM suffixes
[Fpath, Ffile, Fext] = fileparts(InputPath);

[Ind1, Ind2] = regexp(Fext, ',\d+');
if ~isempty(Ind1)
SuffixSPM = InputPath(Ind1:Ind2);
InputPath = InputPath(1:Ind1-1);
if Ind2~=length(Fext)
error('Something wrong with this file extension');
else
SuffixSPM = Fext(Ind1:Ind2);
InputPath = fullfile(Fpath, [Ffile Fext(1:Ind1-1)]);
end
else
SuffixSPM = '';
end

% Now do our core business
%% 2. Manage .gz (double) extensions
[Fpath, Ffile, Fext] = fileparts(InputPath);
[~, Ffile2, Fext2] = fileparts(Ffile);
[~, Ffile2, Fext2] = fileparts(Ffile);

if strcmpi(Fext,'.gz') && ~isempty(Fext2)
Ffile = Ffile2;
Fext = [Fext2, Fext];
if strcmpi(Fext, '.gz') && ~isempty(Fext2)
Ffile = Ffile2;
Fext = [Fext2 Fext];
end

% Put the SuffixSPM back
Fext = [Fext SuffixSPM];

%% 3. Manage folders differently than files
if exist(InputPath, 'dir')
% when running this for a folder, there is no extension, and any
% dot in the name should be ignored and put in the Ffile
% when running this for a folder, there is no extension. So any
% extension should be ignored and put back to the Ffile
Ffile = [Ffile Fext];
Fext = '';
end

end

end

0 comments on commit 1ee59b7

Please sign in to comment.