file name manipulation
38 次查看(过去 30 天)
显示 更早的评论
I have a file name that takes the form: S_1_X_Y where X can be an integer between 1-16, and Y is a letter a,b,c,d.
I want to be able to just replace the number X by a user defined value.
any help greatly appreciated. Thanks
采纳的回答
Oleg Komarov
2011-5-6
X = 1;
Y = 'a';
sprintf('S_1_%d_%s',X,Y)
EDIT
str = 'S_1_6_a'
x = 3;
regexprep(str,'(_)\d+(_[abcd])', ['$1' sprintf('%d',x) '$2'])
ans =
S_1_3_a
更多回答(1 个)
Teja Muppirala
2011-5-6
NUM2STR is useful for stuff like this.
Y = {'a' 'b' 'c' 'd'}
for jj = 1:4
for n = 1:9
filename = ['S_1_' num2str(n) '_' Y{jj}]
end
end
Or alternatively, if you need to have zeros in there like '001', '002', '003' then you can use SPRINTF instead of NUM2STR
n = 7;
filename = ['S_1_' sprintf('%03.f',n) '_a']
n = 61;
filename = ['S_1_' sprintf('%03.f',n) '_a']
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!