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

采纳的回答

Jan
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
Azzi Abdelmalek 2014-3-17
out=cellfun(@(x) x{1},regexp(filename,'[^\.]+(?=\.(.+))','match'),'un',0)

Andrei Bobrov
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{:}];

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by