这里介绍两种方法,各有优点
一, strsplit函数,功能强大
save_path = 'H:\Experiment\Img\';
smap_list = 'P1_front_img.jpg';
cell_str = strsplit(smap_list,'_'); %分成三段: 'P1' 'front' 'img.jpg'
smap_name = cell_str{1,1};
save_full_path = strcat(save_path, smap_name, '.jpg'); % H:\Experiment\Img\P1.jpg
灵活使用strsplit函数,可以截取任意位置的字符串。
二,利用索引,功能简单直接
因为字符串索引是从1开始,最后的索引是end,这里举个栗子:字符串是000_HC_Annotation.png,目的是去掉后面的_Annotation.png,具体实现如下。
img_name = '000_HC_Annotation.png'; # 000_HC_Annotation.png
new_name = img_name(1:end-15); # 这样就去掉了_Annotation.png字符串,新的名字:000_HC