|
1 | 1 | function p = parse_filename(filename, fields)
|
| 2 | + % |
2 | 3 | % Split a filename into its building constituents
|
3 |
| - % FORMAT p = bids.internal.parse_filename(filename, fields) |
| 4 | + % |
| 5 | + % USAGE:: |
| 6 | + % |
| 7 | + % p = bids.internal.parse_filename(filename, fields) |
| 8 | + % |
| 9 | + % :param filename: fielname to parse that follows the pattern |
| 10 | + % ``sub-label[_entity-label]*_suffix.extension`` |
| 11 | + % :type filename: string |
| 12 | + % :param fields: cell of strings of the entities to use for parsing |
| 13 | + % :type fields: cell |
4 | 14 | %
|
5 | 15 | % Example:
|
6 | 16 | %
|
7 |
| - % >> filename = '../sub-16/anat/sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz'; |
8 |
| - % >> bids.internal.parse_filename(filename) |
| 17 | + % filename = '../sub-16/anat/sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz'; |
9 | 18 | %
|
10 |
| - % ans = |
| 19 | + % bids.internal.parse_filename(filename) |
| 20 | + % |
| 21 | + % ans = |
11 | 22 | %
|
12 | 23 | % struct with fields:
|
13 | 24 | %
|
14 |
| - % filename: 'sub-16_ses-mri_run-1_echo-2_FLASH.nii.gz' |
15 |
| - % type: 'FLASH' |
16 |
| - % ext: '.nii.gz' |
17 |
| - % sub: '16' |
18 |
| - % ses: 'mri' |
19 |
| - % run: '1' |
20 |
| - % echo: '2' |
| 25 | + % 'filename', 'sub-16_ses-mri_run-1_acq-hd_T1w.nii.gz', ... |
| 26 | + % 'suffix', 'T1w', ... |
| 27 | + % 'ext', '.nii.gz', ... |
| 28 | + % 'entities', struct('sub', '16', ... |
| 29 | + % 'ses', 'mri', ... |
| 30 | + % 'run', '1', ... |
| 31 | + % 'acq', 'hd'); |
| 32 | + % |
21 | 33 | % __________________________________________________________________________
|
22 | 34 |
|
23 | 35 | % Copyright (C) 2016-2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging
|
|
26 | 38 | filename = bids.internal.file_utils(filename, 'filename');
|
27 | 39 |
|
28 | 40 | % -Identify all the BIDS entity-label pairs present in the filename (delimited by "_")
|
29 |
| - % https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html |
30 | 41 | [parts, dummy] = regexp(filename, '(?:_)+', 'split', 'match'); %#ok<ASGLU>
|
31 | 42 | p.filename = filename;
|
32 | 43 |
|
33 | 44 | % -Identify the suffix and extension of this file
|
34 |
| - % https://bids-specification.readthedocs.io/en/stable/02-common-principles.html#file-name-structure |
35 |
| - [p.type, p.ext] = strtok(parts{end}, '.'); |
| 45 | + [p.suffix, p.ext] = strtok(parts{end}, '.'); |
36 | 46 |
|
37 | 47 | % -Separate the entity from the label for each pair identified above
|
38 | 48 | for i = 1:numel(parts) - 1
|
39 | 49 | [d, dummy] = regexp(parts{i}, '(?:\-)+', 'split', 'match'); %#ok<ASGLU>
|
40 |
| - p.(d{1}) = d{2}; |
| 50 | + p.entities.(d{1}) = d{2}; |
41 | 51 | end
|
42 | 52 |
|
43 | 53 | % -Extra fields can be added to the structure and ordered specifically.
|
44 | 54 | if nargin == 2
|
45 | 55 | for i = 1:numel(fields)
|
46 |
| - if ~isfield(p, fields{i}) |
47 |
| - p.(fields{i}) = ''; |
48 |
| - end |
| 56 | + p.entities = bids.internal.add_missing_field(p.entities, fields{i}); |
49 | 57 | end
|
50 | 58 | try
|
51 |
| - p = orderfields(p, ['filename', 'ext', 'type', fields]); |
| 59 | + p = orderfields(p, {'filename', 'ext', 'suffix', 'entities'}); |
| 60 | + p.entities = orderfields(p.entities, fields); |
52 | 61 | catch
|
53 |
| - warning('Ignoring file ''%s'' not matching template.', filename); |
| 62 | + warning('bidsMatlab:noMatchingTemplate', ... |
| 63 | + 'Ignoring file %s not matching template.', filename); |
54 | 64 | p = struct([]);
|
55 | 65 | end
|
56 | 66 | end
|
| 67 | + |
| 68 | +end |
0 commit comments