- Input Handling: Ensure the inputs for the axis and rotation angle are correctly captured. The axis should be a character ('x', 'y', or 'z'), and the rotation should be a numerical value representing the angle in radians.
- Rotation Matrix Calculation: Use conditional statements to determine which rotation matrix to compute based on the axis specified through the user.
- Matrix Printing: Use the "fprintf" function to correctly format and print the rotation matrix. Since "fprintf" is not directly suitable for printing matrices, you can use a loop or the "disp" function for better formatting.
How to get a single output from a function with 2 inputs and 3 possible outputs depending on the choice made for inputs.
1 view (last 30 days)
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?
0 Comments
Accepted Answer
akshatsood
on 17 Sep 2024
Edited: akshatsood
on 17 Sep 2024
I understand that you want to create a MATLAB function that computes the rotation matrix for a given axis and angle, similar to the "rotx", "roty", and "rotz" commands from the Robotics Toolbox. The function should print the rotation matrix for the specified axis and angle.
Please refer to the steps outlined below that demonstrate the proposed approach.
function rotmat = rot(axis, rotation)
% Calculate the rotation matrix based on the specified axis
if axis == 'x'
rotmat = [1, 0, 0;
0, cos(rotation), -sin(rotation);
0, sin(rotation), cos(rotation)];
elseif axis == 'y'
rotmat = [cos(rotation), 0, sin(rotation);
0, 1, 0;
-sin(rotation), 0, cos(rotation)];
elseif axis == 'z'
rotmat = [cos(rotation), -sin(rotation), 0;
sin(rotation), cos(rotation), 0;
0, 0, 1];
else
error('Invalid axis. Choose x, y, or z.');
end
% Display the rotation matrix
disp('Rotation matrix is:');
disp(rotmat);
end
Usage - To use this function, call it from the MATLAB command window or a script:
axis = input('axis where the rotation is about: ', 's');
rotation = input('angle from first Frame to new frame (in radians): ');
rotmat = rot(axis, rotation);
This will compute and display the appropriate rotation matrix based on user inputs
I hope this helps.
0 Comments
More Answers (2)
Voss
on 17 Sep 2024
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
Voss
on 17 Sep 2024
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
dpb
on 17 Sep 2024
Edited: dpb
on 17 Sep 2024
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
dpb
on 17 Sep 2024
Edited: dpb
on 18 Sep 2024
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..
See Also
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!