How do you output an frprintf from a for loop all on the same line?

6 次查看(过去 30 天)
input=('Enter number')
for i=1:2:length(name)
a=name(i:1+1);
switch a
case '12'
num='twelve';
case '13'
num='thirteen';
etc.
fprintf(Final num is:%s',num)
I want the output to be: Final num is: twelve thirteen etc. But it comes out: Final num is: twelve Final num is: thirteen

回答(2 个)

Walter Roberson
Walter Roberson 2013-4-12
Before the loop:
count = 0;
inside the loop:
count = count + 1;
and instead of assigning to num, assign to num{count}
Move the fprintf() to after the loop, and change it to:
fprintf('Final num is:');
fprintf(' %s', num{:});
fprintf('\n');

per isakson
per isakson 2013-4-12
You might want to markup the question to make exactly clear what you ask for. (Try the [?Help]-button above the text box.
for ii = 1 : 3
num = 'twelve';
fprintf('Final num is: %s ', num )
end
fprintf('\n')
displays
Final num is: twelve Final num is: twelve Final num is: twelve

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by