Parse cell array into separate cells, with no delimiter
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
Réponse acceptée
Voss
le 8 Juil 2022
fileName = {'11171401'; '22282512'; '33393623'};
[track,cycle,segment] = cellfun(@(x)parse_file_name(x),fileName,'UniformOutput',false)
function [t,c,s] = parse_file_name(fn)
t = fn(1:4);
c = fn([5 6]);
s = fn([7 8]);
end
2 commentaires
Voss
le 8 Juil 2022
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)
% indexing goes down the first column first, giving incorrect results
track = str2num(X(1:4))
cycle = str2num(X(5:6))
segment = str2num(X(7:8))
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur String Parsing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!