Parse cell array into separate cells, with no delimiter

Hello! I am trying to parse the name of a file into separate variables to describe the file, but there are no delimiters in the file name. For example, if my file name is
fileName = {'11171401'}
I want to divide this into:
track = {'1117'}
cycle = {'14'}
segment = {'01'}
I've already chopped off the beginning of the file name using the split function, but I can't seem to figure out how to divide up the rest without a delimiter. Thank you in advance for any help!

6 Comments

What are the rules for the name splitting? If it is always 4-2-2 character splits then you could just use indexing.
James Tursa, It is a 4-2-2 character split, but I'm not sure how to get around the fact that fileName is saved as a 1x1 cell.
I found a solution using 'cell2mat' and 'str2num'!
fileName = {'11171401'};
X = cell2mat(fileName);
track = str2num(X(1:4));
cycle = str2num(X(5:6));
segement = str2num(X(7:8));
In your solution, track, cycle and segment are all double, not cells containing character vectors as required. Try
fileName = {'11171401'};
track = {fileName{1}(1:4)}
cycle = {fileName{1}(5:6)}
segment = {fileName{1}(7:8)}
fnm = {'11171401'; '22282512'; '33393623'};
tkn = regexp(fnm,'^(\d{4})(\d\d)(\d\d)$','tokens','once');
tkn = vertcat(tkn{:})
tkn = 3×3 cell array
{'1117'} {'14'} {'01'} {'2228'} {'25'} {'12'} {'3339'} {'36'} {'23'}

Sign in to comment.

 Accepted Answer

fileName = {'11171401'; '22282512'; '33393623'};
[track,cycle,segment] = cellfun(@(x)parse_file_name(x),fileName,'UniformOutput',false)
track = 3×1 cell array
{'1117'} {'2228'} {'3339'}
cycle = 3×1 cell array
{'14'} {'25'} {'36'}
segment = 3×1 cell array
{'01'} {'12'} {'23'}
function [t,c,s] = parse_file_name(fn)
t = fn(1:4);
c = fn([5 6]);
s = fn([7 8]);
end

2 Comments

Thank you! I was trying to avoid having to write a function, but I'm wondering if this is more efficient (less computationally expensive) than the way I did it...
You're welcome!
The way you did it works only for a scalar cell array fileName. Here's testing it on a non-scalar cell array:
fileName = {'11171401'; '22282512'; '33393623'};
X = cell2mat(fileName)
X = 3×8 char array
'11171401' '22282512' '33393623'
% indexing goes down the first column first, giving incorrect results
track = str2num(X(1:4))
track = 1231
cycle = str2num(X(5:6))
cycle = 23
segment = str2num(X(7:8))
segment = 12
Also, your way gives numeric results, but my answer gives cell arrays of character vectors, as specified in the question:
track = {'1117'}
cycle = {'14'}
segment = {'01'}
So since the two methods are really doing completely different things, it's not meaningful to try to compare their relative efficiency.

Sign in to comment.

More Answers (0)

Asked:

on 7 Jul 2022

Edited:

on 8 Jul 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!