Azzera filtri
Azzera filtri

How to get a single output from a function with 2 inputs and 3 possible outputs depending on the choice made for inputs.

60 visualizzazioni (ultimi 30 giorni)
%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?

Risposta accettata

akshatsood
akshatsood il 17 Set 2024 alle 18:21
Modificato: akshatsood il 17 Set 2024 alle 18:22
Hi @Jon,
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.
  1. 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.
  2. Rotation Matrix Calculation: Use conditional statements to determine which rotation matrix to compute based on the axis specified through the user.
  3. 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.
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.

Più risposte (2)

Voss
Voss il 17 Set 2024 alle 18:22
Here's one option:
rot('x',pi/3);
rotation matrix is [1 0 0;0 0.5 -0.866025403784439;0 0.866025403784439 0.5]
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);
rotation matrix is 1.0000 0.0000 0.0000 0.0000 0.5000 -0.8660 0.0000 0.8660 0.5000
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 Commento
Voss
Voss il 17 Set 2024 alle 18:25
Another option:
rot('x',pi/3);
rotation matrix is 1.0000 0 0 0 0.5000 -0.8660 0 0.8660 0.5000
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

Accedi per commentare.


dpb
dpb il 17 Set 2024 alle 18:35
Modificato: dpb il 17 Set 2024 alle 19:16
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)
mx = 3×3
1.0000 0 0 0 0.8660 -0.5000 0 0.5000 0.8660
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
my = rot("y",-30')
my = 3×3
0.8660 0 -0.5000 0 1.0000 0 0.5000 0 0.8660
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mz = rot({'z'},45)
mz = 3×3
0.7071 -0.7071 0 0.7071 0.7071 0 0 0 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mXY=rot('xy',0)
Error using assert
axis must single character 'x', 'y', or 'z'

Error in solution>rot (line 10)
assert((strlength(ax)==1),"axis must single character 'x', 'y', or 'z'") % make sure valid axis direction
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 Commento
dpb
dpb il 17 Set 2024 alle 21:54
Modificato: dpb il 18 Set 2024 alle 14:11
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
rotmat = 3×3
1.0000 0 0 0 0.8660 -0.5000 0 0.5000 0.8660
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
1.0000 0 0 0 0.8660 -0.5000 0 0.5000 0.8660
format bank
disp(rotmat)
1.00 0 0 0 0.87 -0.50 0 0.50 0.87
which also follows the present format. as the above illustrates..

Accedi per commentare.

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by