25

I have a vector x = (1, 2, 3) and I want to display (print) it as Answer: (1, 2, 3).

I have tried many approaches, including:

disp('Answer: ')
strtrim(sprintf('%f ', x))

But I still can't get it to print in format which I need.

Could someone point me to the solution, please?

EDIT: Both the values and (length of) x are not known in advance.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86

6 Answers6

36

I prefer the following, which is cleaner:

x = [1, 2, 3];
g=sprintf('%d ', x);
fprintf('Answer: %s\n', g)

which outputs

Answer: 1 2 3
Uri Cohen
  • 3,488
  • 1
  • 29
  • 46
  • The problem with this answer is that for a matrix, all the rows will get lined up. – Pedro77 Mar 31 '16 at 16:32
  • Right, sprintf can only be used with an array argument, so matrices get flattened. You should apply this to each row and concatenate the result. – Uri Cohen Mar 31 '16 at 19:05
8

You can use

x = [1, 2, 3]
disp(sprintf('Answer: (%d, %d, %d)', x))

This results in

Answer: (1, 2, 3)

For vectors of arbitrary size, you can use

disp(strrep(['Answer: (' sprintf(' %d,', x) ')'], ',)', ')'))

An alternative way would be

disp(strrep(['Answer: (' num2str(x, ' %d,') ')'], ',)', ')'))
H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • 2
    `fprintf('%s%s\b\b%s\n', 'Answer: (', sprintf('%d, ',x), ')');` seems more clear, and does not output space after `(`. – Roman Shapovalov Feb 18 '13 at 14:25
  • @RomanShapovalov: I tried to avoid the backspace `\b` because if you capture the output to a file, it's going to contain literal backspaces. – H.Muster Feb 18 '13 at 14:37
  • Makes sense. You can modify your code to print `'%d, '` (note the position of the space) and then replace `', )'` with `')'` to avoid an excessive space. – Roman Shapovalov Feb 19 '13 at 08:12
  • seeing `,') ')'], ',)', ')'))` in a code makes me shudder, the effort to tell which of those are in strings and which are part of the code is ... *shudders* – Tadhg McDonald-Jensen Jan 19 '18 at 23:51
5

Here's another approach that takes advantage of Matlab's strjoin function. With strjoin it's easy to customize the delimiter between values.

x = [1, 2, 3];
fprintf('Answer: (%s)\n', strjoin(cellstr(num2str(x(:))),', '));

This results in: Answer: (1, 2, 3)

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Andrew H
  • 321
  • 4
  • 9
1

You might try this way:

fprintf('%s: (%i,%i,%i)\r\n','Answer',1,2,3)

I hope this helps.

fpe
  • 2,700
  • 2
  • 23
  • 47
  • Thanks for your answer, but I don't know the answer before running the program. Thought, it is obvious. Also, I don't even know the length of the resulting vector. So I need some general solution. – Edward Ruchevits Feb 17 '13 at 18:36
  • 3
    sorry, I just answered your post: I think you may somehow state you look for a general approach to the problem. – fpe Feb 17 '13 at 18:40
1

Here is a more generalized solution that prints all elements of x the vector x in this format:

x=randperm(3);
s = repmat('%d,',1,length(x));
s(end)=[]; %Remove trailing comma

disp(sprintf(['Answer: (' s ')'], x))
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

To print a vector which possibly has complex numbers-

fprintf('Answer: %s\n', sprintf('%d ', num2str(x)));
raghavsood33
  • 749
  • 7
  • 17