Search code examples
matlabprintfdisp

How to display (print) vector in Matlab?


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.


Solution

  • 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