Reading vector elements into a string

I have a vector of numbers that I would like to use to create a filename. For example with a vector [1] I would like the filename temp0001.file. This is simply done by
filename = sprintf('%04d', vector);
This doesn't carry over once I append more elemnts to the matrix, for example [1,2] should result in temp0012.file but instead becomes temp0102.file. Is there another way to parse the vectors?

2 个评论

If [1,2] should produce 0012, then what should [1,12] produce ?
Sorry I have assumed the contents of the vector are integers >-1 and <10

请先登录,再进行评论。

回答(2 个)

temp = sprintf('%d', [0 0 0 0, vector]);
temp = temp(end-3:end);
filename = sprintf('temp%s.file', temp);
n = [1 2] ; % your vector
v = sprintf('%.0f' , n) ; % convert n to char
filename = sprintf('%04d',str2num(v));

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

产品

版本

R2019b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by