Reading vector elements into a string
    4 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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 件のコメント
回答 (2 件)
  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
      
      
 2020 年 7 月 23 日
				filename = sprintf('temp%04d.file', sum(vector .* 10.^(length(vector)-1:-1:0)));
  KSSV
      
      
 2020 年 7 月 23 日
        n = [1 2] ;  % your vector
v = sprintf('%.0f' , n) ;  % convert n to char 
filename = sprintf('%04d',str2num(v));
0 件のコメント
参考
カテゴリ
				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!


