how to get the first 3 characters of each element in a cell to another cell
2 次查看(过去 30 天)
显示 更早的评论
filename={'CD2.2008052716-17hZCFPF.GY.txt', 'GOM.2008052716-17hZCFPF.GY.txt','GYA.2008052716-17hZCFPF.GY.txt'}
then I want to get a new cell
it would be like {'CD2',"GOM','GYA'};
how to make it
0 个评论
采纳的回答
Jan
2014-3-17
filename = {'CD2.2008052716-17hZCFPF.GY.txt', ...
'GOM.2008052716-17hZCFPF.GY.txt', ...
'GYA.2008052716-17hZCFPF.GY.txt'};
P = strtok(filename, '.')
更多回答(2 个)
Azzi Abdelmalek
2014-3-17
out=cellfun(@(x) x{1},regexp(filename,'[^\.]+(?=\.(.+))','match'),'un',0)
1 个评论
Jan
2014-3-17
Or without cellfun:
regexp(filename,'[^\.]+(?=\.(.+))', 'match', 'once')
Andrei Bobrov
2014-3-17
In the general case:
filename = {'CZXDD2.2008052716-17hZCFPF.GY.txt', 'GOM.2008052716-17hZCFPF.GY.txt','GY3SAA9.2008052716-17hZCFPF.GY.txt'};
out = regexp(filename,'^\w*(?=\.)','match');
out = [out{:}];
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!