-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFilenameBody.m
61 lines (54 loc) · 1.48 KB
/
getFilenameBody.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [path,body,no,ext]=getFilenameBody(fname)
%GETFILENAMEBODY splits a filename into its body, number and
% extension part
%
% SYNOPSIS [path,body,no,ext]=getFilenameBody(fname)
%
% INPUT fname : filename; the following filename structure must be
% preserved:
% - alphanumeric body
% - number before extension
% - extension separated
%
% OUTPUT path : string with the path, [] if non-existent
% body : string with body, [] if non-existent
% no : string with the number, [] if non-existent
% ext : extension, [] if non-existent
%
% SAMPLE getFileNameBody('test1.tif') returns
% []
% 'test',
% '1',
% '.tif'
%
% getFileNameBody('C:\mydir\test1.tif') returns
% 'C:\mydir'
% 'test',
% '1',
% '.tif'
%
% SEE ALSO fileparts
% initialize
path = [];
body = [];
no = [];
ext = [];
% search for extension
[path,name,ext] = fileparts(fname);
% search for letters in remainder
idxs=length(name);
while(uint8(name(idxs))>47 & uint8(name(idxs))<58)
idxs = idxs-1;
end;
% check whether this index points to an actual number
if( length(name) ~= idxs )
no = name((idxs+1):length(name));
if(isempty(str2num(no)))
no = [];
error('unsupported filename format entered');
else
body = name(1:idxs);
end;
else
body = name;
end;