Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

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

Asked by Jake

Jake (view profile)

on 12 Apr 2013

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

0 Comments

Jake

Jake (view profile)

Tags

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson

Walter Roberson (view profile)

on 12 Apr 2013

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');

0 Comments

Walter Roberson

Walter Roberson (view profile)

Answer by per isakson

per isakson (view profile)

on 12 Apr 2013

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 

0 Comments

per isakson

per isakson (view profile)

Contact us