The rotation in the rectangle function in matlab cannot be used, but the 2022 version can be used.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
rectangle('Position',[1 2 5 6],'Rotation',45);
axis([0 10 0 10]);

Accepted Answer
Steven Lord
on 20 Sep 2024
try
rectangle('Position',[1 2 5 6],'Rotation',45);
catch ME
fprintf("This command threw the following error:\n%s", ME.message)
end
This command threw the following error:
Unrecognized property Rotation for class Rectangle.
The word Rotation does not appear at all on the documentation page listing the properties for the object created by the rectangle function.
There is an object in Image Processing Toolbox, images.roi.Rectangle, that has rotation-related properties. Did you meant to create one of those?
obj = images.roi.Rectangle('Position',[1 2 5 6],'Rotation',45)
obj =
Rectangle with properties:
Position: [1 2 5 6]
RotationAngle: 45
AspectRatio: 1.2000
Label: ''
Use GET to show all properties
There are a number of other functions that show up when I search the documentation for "rectangle rotation" -- if you meant to use one of those please clarify which type of object you're trying to use and we may be able to suggest the right approach (or tell you that what you're trying to do isn't supported.)
5 Comments
道夫
on 21 Sep 2024
thank you
道夫
on 21 Sep 2024
I want to try to achieve this effect

道夫
on 21 Sep 2024
I want the rotated rectangles to be arranged periodically, but with the above code I don’t know how to loop and arrange the rectangles at different positions.
The rotate() function does not work on rectangle objects.
hr = rectangle('Position',[1 2 5 6]);
rotate(hr,[0 0 1],45) % does nothing
axis equal

You can use hgtransform on rectangle objects:
% initial rectangle parameters
rsize = [5 7]; % [w h]
center = [3.5 5]; % [x y]
% cycle parameters
offset = [8 0]; % [x y]
anglerange = [0 90];
nsteps = 7;
% draw a sequence of rectangles
angles = linspace(anglerange(1),anglerange(2),nsteps);
g = gobjects(nsteps,1);
hr = gobjects(nsteps,1);
for k = 1:nsteps
g(k) = hgtransform;
hr(k) = rectangle('Position',[-rsize/2 rsize],'parent',g(k),'facecolor','r');
Mrot = makehgtform('zrotate',deg2rad(angles(k)));
Mtrans = makehgtform('translate',[(center + (k-1)*offset) 0]);
g(k).Matrix = Mtrans*Mrot;
hold on
end
axis equal

... or you can use rotate() on patch() objects (or polyshapes)
% initial rectangle parameters
rsize = [5 7]; % [w h]
center = [3.5 5]; % [x y]
% cycle parameters
offset = [8 0]; % [x y]
anglerange = [0 90];
nsteps = 7;
% draw a sequence of rectangles
angles = linspace(anglerange(1),anglerange(2),nsteps);
hr = gobjects(nsteps,1);
for k = 1:nsteps
thisc = center + (k-1)*offset;
vx = thisc(1) + [-1 1 1 -1]*rsize(1)/2;
vy = thisc(2) + [-1 -1 1 1]*rsize(2)/2;
hr(k) = patch(vx,vy,'r');
rotate(hr(k),[0 0 1],angles(k),[thisc 0]);
hold on
end
axis equal

See also:
道夫
on 23 Sep 2024
thank you very much
More Answers (0)
Categories
Find more on Animation in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)