how to rotate a graphic object using a button press

5 views (last 30 days)
Hi, im trying to rotate a graphic object using a button press but i can understand what im doing wrong. the code is running but not responding to keypress. basicaly im drawing a 3d star in different colors and i want to allow it to rotete in one direction when pressing + and in the oter direction if pressing any other key. and end if i press escape. I have a feeling im not using KeyPressFcn right but couldnt understand what im doing wrong.
here is my code if true % code function main() clear all; clc; close all;
fig=figure('KeyPressFcn',@keypress); ax = axes('XLim',[-1.5 1.5],'YLim',[-1.5 1.5],... 'ZLim',[-1.5 1.5]); view(3); grid off; axis off ; whitebg([0 .5 .6]); daspect([1,1,1]); [x, y, z] = cylinder([.2 0]); h(1) = surface(x,y,z,'FaceColor','red'); h(2) = surface(x,y,-z,'FaceColor','green'); h(3) = surface(z,x,y,'FaceColor','blue'); h(4) = surface(-z,x,y,'FaceColor','cyan'); h(5) = surface(y,z,x,'FaceColor','magenta'); h(6) = surface(y,-z,x,'FaceColor','yellow'); t = hgtransform('Parent',ax); set(h,'Parent',t) set(gcf,'Renderer','opengl') drawnow
Rz = eye(4); flag=0; angle=0; while flag==0 % Z-axis rotation matrix Rz = makehgtform('zrotate',angle); % Concatenate the transforms and % set the hgtransform Matrix property set(t,'Matrix',Rz) waitforbuttonpress ; drawnow; end
function keypress(src,event) fprintf('im here') if strcmp(event.Character,'escape') flag=1; else if event.Character == '+' angle=angle+5; else angle=angle-5; end end end end end

Answers (0)

Categories

Find more on Graphics Performance 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!