Trying to print 2 different variables using fprintf.

42 views (last 30 days)
I have 2 matrix, a=[1 2 3;4 5 6;n n n] and b=[11 22 33;44 55 66;nn nn nn] I am trying to print the 1st variable of a and b then the next etc:
fprintf('A is %6.4f and b is %6.4f', a, b);
the way math lab is working gives an output of:
A is 1 and b is 2
A is 3 and b is 11
A is 22 and b is 33
How can i separate it so it shows
A is 1 and b is 11
A is 2 and b is 22
A is 3 and b is 33
A is n and b is nn

Accepted Answer

Walter Roberson
Walter Roberson on 6 Feb 2014
fprintf('A is %6.4f and b is %6.4f', [a(:), b(:)].' );
Note: this will not print in the order you indicated because in MATLAB, the second variable of "a" is the 4, not the 2. If you want to print across the rows instead of down the columns, then
fprintf('A is %6.4f and b is %6.4f', permute(cat(3,a,b), [3 2 1]));
and the first version, taking the values in column order, can be done as
fprintf('A is %6.4f and b is %6.4f', permute(cat(3,a,b), [3 1 2]));

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!