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.

6 Answers 6

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
2
  • The problem with this answer is that for a matrix, all the rows will get lined up.
    – Pedro77
    Mar 31, 2016 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, 2016 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,') ')'], ',)', ')'))
4
  • 2
    fprintf('%s%s\b\b%s\n', 'Answer: (', sprintf('%d, ',x), ')'); seems more clear, and does not output space after (. Feb 18, 2013 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, 2013 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. Feb 19, 2013 at 8: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 Jan 19, 2018 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)

1

You might try this way:

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

I hope this helps.

2
  • 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. Feb 17, 2013 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, 2013 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))
3
  • Shouldn't the last line just read disp(['Answer: (' s ')']);?
    – H.Muster
    Feb 18, 2013 at 13:39
  • @H.Muster That would not pass the values of x, it would print: Answer: (%d,%d,%d). Note that the repmat just creates the syntax Feb 18, 2013 at 14:02
  • You are right; I misread the second line of your code and therefore misjudged the last line.
    – H.Muster
    Feb 18, 2013 at 14:05
0

To print a vector which possibly has complex numbers-

fprintf('Answer: %s\n', sprintf('%d ', num2str(x)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.