why do the markers change size after several key press

2 Ansichten (letzte 30 Tage)
Itzik Ben Shabat
Itzik Ben Shabat am 26 Aug. 2013
Bearbeitet: John Kelly am 10 Nov. 2017
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

Akzeptierte Antwort

Itzik Ben Shabat
Itzik Ben Shabat am 26 Aug. 2013
Bearbeitet: Itzik Ben Shabat am 26 Aug. 2013
so I solved the problem by adding this line of code in the main function
set(gcf, 'Renderer', 'zbuffer');
I guess there is some sort of problem with the default MATLAB renderer. Just thought to post this so whoever encounters this in the fture will know. does anyone know what the real problem is ? thanks

Weitere Antworten (1)

Jan
Jan am 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.
  2 Kommentare
Itzik Ben Shabat
Itzik Ben Shabat am 26 Aug. 2013
Hi Jan,
First I apologize for the ugliness. i did not want to ask a general question without attaching the example.
I know what the comment means because i wrote it. I tried debugging but i stil cant unerstand whats causing this (removed the line as you said and used break points etc.).
I pasted the code just so you can use it and see the weirdness. should i attach a file in the future instead of the code?
the only new thing i noticed is that it happens after a certain amount of key presses (53 '+' from execution)
do you know whats causing this?
Jan
Jan am 26 Aug. 2013
Thanks for your explanations. Posting the code with a proper formatting is fine. I cannot run Matlab currently, such that I cannot find out, which line causes the changes.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by