开发者

Prefix match in MATLAB

Hey guys, I have a very simple problem in MATLAB:

I have some string开发者_C百科s which are like this:

Pic001
Pic002
Pic003
004

Not every string starts with the prefix "Pic". So how can I cut off the part "pic" that only the numbers at the end shall remain to have an equal format for all my strings?

Greets, poeschlorn


If 'Pic' only ever occurs as a prefix in your strings and nowhere else within the strings then you could use STRREP to remove it like this:

>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}

x = 

    'Pic001'
    'Pic002'
    'Pic003'
    '004'

>> x = strrep(x, 'Pic', '')

x = 

    '001'
    '002'
    '003'
    '004'

If 'Pic' can occur elsewhere in your strings and you only want to remove it when it occurs as a prefix then use STRNCMP to compare the first three characters of your strings:

>> x = {'Pic001'; 'Pic002'; 'Pic003'; '004'}

x = 

    'Pic001'
    'Pic002'
    'Pic003'
    '004'

>> for ii = find(strncmp(x, 'Pic', 3))'
x{ii}(1:3) = [];
end
>> x

x = 

    '001'
    '002'
    '003'
    '004'


strings = {'Pic001'; 'Pic002'; 'Pic003'; '004'};
numbers = regexp(strings, '(PIC)?(\d*)','match');
for cc = 1:length(numbers); 
    fprintf('%s\n', char(numbers{cc}));
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜