how to use sprintf

6 次查看(过去 30 天)
Hale
Hale 2013-7-17
Hi,
I have problems in defining the right format in sprintf. I want to enter subfolders with names defined in the n vector. I used sprintf('%1.1f', j*0.1) to define the name of the folder but it reads the folders like [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,...] and the problem is that my folder is named 1 and not 1.0. How to I specify it in the format of sprintf?
n = [0.1:0.1:120]';
for j=1:length(n)
folder = sprintf('%1.1f', j*0.1);
cd (folder);
end
thanks

回答(1 个)

Jan
Jan 2013-7-17
But is this surprising? You multiply j by 0.1 and if j is 1 the resulting value is 0.1 .
You showed code, which does not work and ask for improvements. But you did not explain, what you want. So how could we suggest anything without guessing?
What about this guess:
for j = 1:120
folder = sprintf('%d', j);
cd(folder);
end
But this would mean, that you have a folder hierarchy like 1\2\3\4\5\...

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by