How do I get fprintf to display several matrices at once while maintaining the desired order?

10 Ansichten (letzte 30 Tage)
I'm having trouble getting the fprintf function to give me the output I want for a practice function I'm writing. The function is designed to accept vector input and convert the values into polar coordinates. It works fine, except that the line at the end where I want to display everything (x,y,r,and theta) in a labeled table rearranges their order. Allow me to use my code to demonstrate.
First, I should clarify that I'm using this as my input:
v =
1 0
1 1
0 1
-1 1
-1 0
-1 -1
0 0
0 -1
1 -1
function M = polarv( v )
%Converts Cartesian coordinates into polar coordinates
%input:
% v = the vectors input into Matlab in i-j form
%output:
% r = radius
% theta = angle with horizontal in degrees
x = v(:,1);
y = v(:,2);
r = [];
angle = [];
for i = 1:length(v);
r = sqrt(x.^2+y.^2);
if (x(i)>0)
angle(i) = atan(y(i)/x(i));
elseif ( x(i) < 0 & y(i) > 0)
angle(i) = atan(y(i)/x(i))+pi;
elseif (x(i) == 0 & y(i) < 0)
angle(i) = -pi/2;
elseif ( x(i) == 0 & y(i) > 0)
angle(i) = pi/2;
elseif (x(i) < 0 & y(i) == 0)
angle(i) = pi;
elseif (x(i) < 0 & y(i) < 0)
angle(i) = atan(y(i)/x(i))-pi;
else
angle(i) = 0;
end
end
theta = (angle*180/pi)';
M = [r,theta];
A = [x,y,r,theta];
fprintf(' x y r theta\n');
A
%fprintf(' %4.3f %4.3f %6.3f %4.2f\n', A);
end
The 'A' at the end is a workaround, to display those values properly because the final commented section isn't working as I would like it to. Here is what I want to see:
1.0000 0 1.0000 0
1.0000 1.0000 1.4142 45.0000
0 1.0000 1.0000 90.0000
-1.0000 1.0000 1.4142 135.0000
-1.0000 0 1.0000 180.0000
-1.0000 -1.0000 1.4142 -135.0000
0 0 0 0
0 -1.0000 1.0000 -90.0000
1.0000 -1.0000 1.4142 -45.0000
Instead, I'm getting this when I run fprintf for A:
1.000 1.000 0.000 -1.00
-1.000 -1.000 0.000 0.00
1.000 0.000 1.000 1.00
1.000 0.000 -1.000 0.00
-1.000 -1.000 1.000 1.41
1.000 1.414 1.000 1.41
0.000 1.000 1.414 0.00
45.000 90.000 135.000 180.00
-135.000 0.000 -90.000 -45.00
As you can see, it is displaying the values of x first, but left-to-right instead of top to bottom. I also have this problem when I use the four column vectors as separate arguments instead of using A. I have also tried using their transverses as inputs, with the same results.

Akzeptierte Antwort

Kevin Claytor
Kevin Claytor am 13 Sep. 2012
My guess would be that this is because MATLAB uses column-major order, you could simply transpose the matrix you are sending to fprintf;
A = rand(5);
A
fprintf('%f %f %f %f %f\n',A') % Works properly

Weitere Antworten (1)

Matt Fig
Matt Fig am 13 Sep. 2012
Bearbeitet: Matt Fig am 13 Sep. 2012
fprintf('\n\n%11s%11s%11s%11s\n','x', 'y', 'r', 'theta');
fprintf(' %10.2f %10.2f %10.2f %10.2f\n', A.');
fprintf('\n\n')
Now check:
>> polarv(v);
x y r theta
1.00 0.00 1.00 0.00
1.00 1.00 1.41 45.00
0.00 1.00 1.00 90.00
-1.00 1.00 1.41 135.00
-1.00 0.00 1.00 180.00
-1.00 -1.00 1.41 -135.00
0.00 0.00 0.00 0.00
0.00 -1.00 1.00 -90.00
1.00 -1.00 1.41 -45.00
>>

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by