1 view (last 30 days)

Show older comments

hi,

my code is attached below. it works but i noticed something odd. after i press '+' a few times the yellow markers shrink. it happens for the other keyboard inputs as well. why is this happening and how can i prevent it?

thanks

function [ ] = mainClock( )

clear all; close all; clc; %not recommended use inside functions

%main3 activates Draw clock function to draw the clock

%Initialize Variables

global toggle

global addhour

global addminutes

toggle=1;

addhour=0;

addminutes=0;

Figureh=0;

Axesh=0;

Objecth=0;

bgcolor='r';

%code

Figureh=figure('NumberTitle','off','Name','Itzik Transformation Clock',...

'Position',[200 200 500 500],'Color','r'); %bg is set to red so we know that we can only see the axes

%Call Input Callbacks

set(Figureh,'KeyPressFcn',@KeyboardCB);

Objecth=DrawingCB();

end

function [handle] = DrawingCB()

%initialize variables

global toggle

global addhour

global addminutes

R=3;

x0=0;

y0=0;

hours=6;

minutes=45;

%code

Axesh=axes('Color','c','Position',[0 0 1 1],'XTick',[],'YTick',[],'XLim',[-R R],'yLim',[-R R]);

handle=DrawClock( Axesh,R,0,0,hours+addhour,minutes+addminutes );

end

function [GroupHandle] = DrawClock( h,R,x0,y0,hours,minutes )

%DrawClock draws a clock of radius R in axes represented by handle h

%INPUT -

%R - clock Radius

%h - axis handle

%hours - hours in 1-12 format

%minutes - minutes 0-59 format

%OUTPUT

%GroupHandle - all of the clocks elements handles grouped

%Initialize Variabls

global toggle

global addhour

global addminutes

GroupHandle=0;

t=0:pi/18:2*pi;

x=R*cos(t)+x0;

y=R*sin(t)+y0;

daspect([1 1 1]);

%Code

set(gcf,'CurrentAxes',h)

%Calculate X,Y coordinates for circle Drawing using t parameter

CircHandle=line(x,y,'Color','b','LineWidth',1,...

'Marker','o','MarkerSize',2,'MarkerFaceColor','y','MarkerEdgeColor','y');

%Plot circle center

hCenter=line(x0,y0,'Marker','s','MarkerFaceColor','y','MarkerEdgeColor','y');

%Plot Dials

hLongDial=line([x0 x0],[y0 y0+R*0.6],'Color','r','LineWidth',3); %long dial

hShortDial=line([x0 x0+0.3*R],[y0 y0],'Color','r','LineWidth',3); %short dial

%calculate matrix

Rminutes=makehgtform('zrotate',-minutes*2*pi/60);

Rhours=makehgtform('zrotate',-hours*2*pi/12+pi/2);

Thandleminutes = hgtransform('Parent',gca,'matrix',Rminutes);

set(hLongDial,'Parent',Thandleminutes);

Thandlehours = hgtransform('Parent',gca,'matrix',Rhours);

set(hShortDial,'Parent',Thandlehours);

end

function [ ] = KeyboardCB(hObject,event)

%pressing will do

% c- toggle between minutes and hours dial

% + - add hours/minutes

%- -subtract hous/minutes

global toggle

global addhour

global addminutes

switch event.Key

case 'c'

if toggle==1

toggle=2;

else

toggle=1;

end

case 'add'

if toggle==1

addhour=addhour+1;

else

addminutes=addminutes+1;

end

case 'subtract'

if toggle==1

addhour=addhour-1;

else

addminutes=addminutes-1;

end

end

DrawingCB();

end

Jan
on 26 Aug 2013

clear all; close all; clc; %not recommended use inside functions

What do you think does this comment mean?? Because clear all removes all breakpoints also, you cannot use the debugger to find out, what's going on. So remove this line at first. Then set some breakpoints in the function and check what happens by stepping through the code line by line. You will find, which line is responsible for the changes and if the problem does not get clear immediately, you can ask a much more precise question in the forum.

Please, Itzik, avoid this ugly piece of code also it is found in so many examples. I assume, that this brute clearing header appears such frequently in the forum, because it makes it much harder to find out by yourself, what's going on in the code.

Jan
on 26 Aug 2013

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

Start Hunting!