
关于matlab读取字符串中部分内容
我要读取一个文件夹里的文件名,但是我只想保存文件名里的一部分,例如文件名a='apple_1_1_1_crop'请问我要怎么只读取apple_1_1_1?也就是读取一个文...
我要读取一个文件夹里的文件名,但是我只想保存文件名里的一部分,例如
文件名a='apple_1_1_1_crop'请问我要怎么只读取apple_1_1_1?
也就是读取一个文件名,遇到'_crop'就只读‘_crop’之前的内容?急求高手解答!!! 展开
文件名a='apple_1_1_1_crop'请问我要怎么只读取apple_1_1_1?
也就是读取一个文件名,遇到'_crop'就只读‘_crop’之前的内容?急求高手解答!!! 展开
3个回答
展开全部
字符串其实可以看成一个矢量,读取部分内容,只要找到想要读取部分的下标,或者需要剔除部分的下标即可。
例如有字符串 'this is just for example'
如果需要得到里面的单词而不需要空格,可以使用如下代码:
str='this is just for example';
i=find(str==' ');
c={str(1:i(1)-1)}; %把所有的单词最后存到c中
for j=1:length(i)-1
c={c{1:j},str(i(j)+1:i(j+1)-1)};
end
c={c{1:j+1},str(i(j+1)+1:end)};
celldisp(c); %列出c中读到的字符串
运行结果如下:
c{1} =
this
c{2} =
is
c{3} =
just
c{4} =
for
c{5} =
example
例如有字符串 'this is just for example'
如果需要得到里面的单词而不需要空格,可以使用如下代码:
str='this is just for example';
i=find(str==' ');
c={str(1:i(1)-1)}; %把所有的单词最后存到c中
for j=1:length(i)-1
c={c{1:j},str(i(j)+1:i(j+1)-1)};
end
c={c{1:j+1},str(i(j+1)+1:end)};
celldisp(c); %列出c中读到的字符串
运行结果如下:
c{1} =
this
c{2} =
is
c{3} =
just
c{4} =
for
c{5} =
example
展开全部
示例如下:
>> a='apple_1_1_1_crop'
a =
apple_1_1_1_crop
>> strfind ( a, '_crop' )
ans =
12
>> str = a( 1 : ans - 1 )
str =
apple_1_1_1
>> a='apple_1_1_1_crop'
a =
apple_1_1_1_crop
>> strfind ( a, '_crop' )
ans =
12
>> str = a( 1 : ans - 1 )
str =
apple_1_1_1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
a='apple_1_1_1_crop';
ind=strfind(a,'_crop');
if ~isempty(ind)
b=a(1:ind(1)-1);
else
b=a;
end
b
ind=strfind(a,'_crop');
if ~isempty(ind)
b=a(1:ind(1)-1);
else
b=a;
end
b
追问
万分感谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询