Reading vector elements into a string

조회 수: 4 (최근 30일)
Morten Nissov
Morten Nissov 2020년 7월 23일
댓글: Walter Roberson 2020년 7월 23일
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
Walter Roberson
Walter Roberson 2020년 7월 23일
If [1,2] should produce 0012, then what should [1,12] produce ?
Morten Nissov
Morten Nissov 2020년 7월 23일
Sorry I have assumed the contents of the vector are integers >-1 and <10

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2020년 7월 23일
temp = sprintf('%d', [0 0 0 0, vector]);
temp = temp(end-3:end);
filename = sprintf('temp%s.file', temp);
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 7월 23일
filename = sprintf('temp%04d.file', sum(vector .* 10.^(length(vector)-1:-1:0)));

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2020년 7월 23일
n = [1 2] ; % your vector
v = sprintf('%.0f' , n) ; % convert n to char
filename = sprintf('%04d',str2num(v));

카테고리

Help CenterFile 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