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
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe %d instead of %f would be better in that case.
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
@jonprasetyo Use normal vector indexing - fprintf('%d\n', x(2))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.