0

I am new to Matlab and I am not sure how to search for what I am trying to achieve.

Say I have a matrix that looks like this:

x = [1;2;3];

How do I output something like:

First row = 1
Second row  = 2
Third row = 3

Many thanks!

1 Answer 1

4

Well, you can always use fprintf, but it will not count the row numbers in english :)

fprintf('row value %d\n', x)

row value 1
row value 2
row value 3

You can also add individual row text, if you insist:

% convert your x vector to a cell matrix
Cx = mat2cell(x, ones(size(x)));

% define individual row texts in a cell matrix
str = {'First  row'; 'Second row'; 'Third  row'};

% print both using cellfun
cellfun(@(s,v)fprintf('%s %d\n', s, v), str, Cx);

First  row 1
Second row 2
Third  row 3
3
  • Maybe %d instead of %f would be better in that case.
    – Oli
    Dec 5, 2012 at 15:37
  • Okay thank you very much! Just wondering is it also possible to just pick one of the numbers? Let's say i just want the second number (2) and printout row value 2 Dec 5, 2012 at 15:45
  • @jonprasetyo Use normal vector indexing - fprintf('%d\n', x(2))
    – angainor
    Dec 5, 2012 at 15:47

Your Answer

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

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