1
votes

So I have a cell containing several strings, e.g.

cell = {'a1', 'b2', 'c3'};

for i = 1:3
    sprintf('%s ', cell{i});
end

I was expecting some out put like a1 b2 c3 , but there was nothing. The funny thing is the sprintf works in the command window (without the semicolon).

Please let me know what got wrong. Thank you so much.

3

3 Answers

1
votes

The sprintf function generates a string. It doesn't output it. The reason you see it in the command window is probably because you don't write the semi-colon, and so the result is shown to you.

You can use fprintf instead, or disp. Look them both up in the MatLab help.

doc fprintf
doc disp
0
votes

Use

fprintf(1, '%s ', cell{i});

or

s = sprintf('%s ', cell{i});
disp(s);
0
votes

I came across the similar problem: use a variable for assignment the output from sprintf replace

sprintf('%s ', cell{i}); 

to

s=sprintf('%s ', cell{i});

Here s will hold the value of cell when looped as in your case