How to get a single output from a function with 2 inputs and 3 possible outputs depending on the choice made for inputs.
Show older comments
%axis->choose which axis the Frames are rotated around
%rotation->specify the angle of rotation between the original Frame and the new Frame
clear,clc
axis=input('axis where the rotation is about', 's')
rotation=input('angle from first Frame to new frame' )
Below finds the Matrices if the robotic tool box is not installed
function rotmat=rot(axis,rotation)
if axis=='x'
rotmat=[ 1 0 0;
0 cos(rotation) -sin(rotation);
0 sin(rotation) cos(rotation)];
end
if axis=='y'
rotmat=[cos(rotation) 0 sin(rotation);
0 1 0;
-sin(rotation) 0 cos(rotation)];
end
if axis=='z'
rotmat=[cos(rotation) -sin(rotation) 0;
sin(rotation) cos(rotation) 0;
0 0 1];
end
fprintf('rotation matrix is %d\n',rotmat)
end
I am trying to create a program that acts like the rotx,roty, or rotz commands. This is incase some one doesn't have the robotic tool box installed, but I can not get it to print out the rotation matrix. How would I get fprintf to print out only the matrix that the frame is rotated about?
Accepted Answer
More Answers (2)
Here's one option:
rot('x',pi/3);
function rotmat=rot(axis,rotation)
switch axis
case 'x'
rotmat=[ 1 0 0;
0 cos(rotation) -sin(rotation);
0 sin(rotation) cos(rotation)];
case 'y'
rotmat=[cos(rotation) 0 sin(rotation);
0 1 0;
-sin(rotation) 0 cos(rotation)];
case 'z'
rotmat=[cos(rotation) -sin(rotation) 0;
sin(rotation) cos(rotation) 0;
0 0 1];
otherwise
error('unrecognized axis. use x, y, or z')
end
fprintf('rotation matrix is %s\n',mat2str(rotmat))
end
Here's another option:
rot('x',pi/3);
function rotmat=rot(axis,rotation)
switch axis
case 'x'
rotmat=[ 1 0 0;
0 cos(rotation) -sin(rotation);
0 sin(rotation) cos(rotation)];
case 'y'
rotmat=[cos(rotation) 0 sin(rotation);
0 1 0;
-sin(rotation) 0 cos(rotation)];
case 'z'
rotmat=[cos(rotation) -sin(rotation) 0;
sin(rotation) cos(rotation) 0;
0 0 1];
otherwise
error('unrecognized axis. use x, y, or z')
end
format_str = '%7.4f';
[m,n] = size(rotmat);
str = repmat([strjoin(repmat({format_str},1,n),' ') '\n'],1,m);
fprintf(['rotation matrix is\n' str],rotmat.')
end
1 Comment
Another option:
rot('x',pi/3);
function rotmat=rot(axis,rotation)
switch axis
case 'x'
rotmat=[ 1 0 0;
0 cos(rotation) -sin(rotation);
0 sin(rotation) cos(rotation)];
case 'y'
rotmat=[cos(rotation) 0 sin(rotation);
0 1 0;
-sin(rotation) 0 cos(rotation)];
case 'z'
rotmat=[cos(rotation) -sin(rotation) 0;
sin(rotation) cos(rotation) 0;
0 0 1];
otherwise
error('unrecognized axis. use x, y, or z')
end
fprintf('rotation matrix is\n%s',formattedDisplayText(rotmat))
end
You don't want to print the matrix to mimic the other functions; what you want is to simply return them...although your function certainly will echo the result to the command line; that isn't going to be useful for an application to use.
Also NOTA BENE that to match the supplied rot? functions, the angle needs to be in degrees and use the "d" trig functions that accept the arguments in degrees. The builtin trig functions without the trailing "d" are in radians and so your function will not reproduce the other functions.
function rotmat=rot(axis,angle)
% replacement for MATLAB rotx, roty, rotz
% USAGE:
% rotmat = rot(axis,angle)
% axis = string or char of axis, |'X'|'Y'|'Z']
% angle= angle to rotate by, degrees
assert(ischar(axis)|iscellstr(axis)|isstring(axis),'axis must be char, cellstr or string')
ax=lower(string(axis)); % convert to string and lower case for consistency
assert((strlength(ax)==1),"axis must single character 'x', 'y', or 'z'") % make sure valid axis direction
assert(matches(ax,["x","y","z"]),"axis must be 'x', 'y', or 'z'") % make sure valid axis direction
s=sind(angle); c=cosd(angle); % compute the sin, cos in degrees
switch ax
case 'x'
rotmat=[ 1 0 0;
0 c -s;
0 s c];
case 'y'
rotmat=[ c 0 s;
0 1 0;
-s 0 c];
case 'z'
rotmat=[ c -s 0;
s c 0;
0 0 1];
end
end
mx = rot('X',30)
my = rot("y",-30')
mz = rot({'z'},45)
mXY=rot('xy',0)
The above is somewhat more flexible in that it accepts char(), cellstr() or string() input of either upper/lower case. It could still use a little more verification and elegant error handling, etc., ..., but would be a reasonable starting point to provide...the above illustrates calling it with each of the possible types of inputs and a bad case at the end...
The "Error in using assert" seems to be a figment of the forum online code; MATLAB does not output that error but displays only the assert() error message and aborts on desktop. Not sure what's up with that here...
1 Comment
ADDENDUM:
Re: the issue about fprintf to display the rotation matrix, I would still assert it will be very annoying to have the function dump its innards to the command line every time it's called, no matter what, so I still think that including that inside the function is abadidea™.
But, it is nice to see the output on occasion, agreed, if it isn't belligerent in doing so. :)
Others have illustrated how to use fprintf to display an array with formatting, but the easy way for just showing what a value is is as simple as
angle=30; s=sind(angle); c=cosd(angle);
rotmat=[ 1 0 0; 0 c -s; 0 s c];
rotmat
The variable name without the trailing semicolon will echo the variable name and content to the command line for you without any explicit formatting required; it will be in the current format as set by the format function.
Alternatively, if you just want the data values without the variable name, then
disp(rotmat)
format bank
disp(rotmat)
which also follows the present format. as the above illustrates..
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!