How to display (print) vector in Matlab?

Edward Ruchevits picture Edward Ruchevits · Feb 17, 2013 · Viewed 107.5k times · Source

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.

Answer

Uri Cohen picture Uri Cohen · Jan 8, 2015

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