Clear Plot without clearing UI elements
Show older comments
I'm planning to plot this function (roulette wheel) onto another figure with UI controls. However, I was wondering if there was a way to clear just the roulette wheel plot (remove it along with the axis) without getting rid of the rest of the UI. I'm calling the roulette wheel using the RW(wNumber) from the main file, where wNumber is the randomly generated winning number.
Roulette wheel function:
function RW(wNumber)
% Set parameters (these could be arguments to a function)
rInner = 150; % inner radius of the colour ring
rOuter = 200; % outer radius of the colour ring
% this is the clockwise sequence for a european pattern single-green wheel
slotnums = [0 32 15 19 4 21 2 25 17 34 6 27 13 36 11 30 8 23 10 5 24 16 33 1 20 14 31 9 22 18 29 7 28 12 35 3 26];
slotColour = [1 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2];
% slotColor used to index colours, 1 - green for 0, 2 - blacks, 3 - reds. Each
% number from slotnums is assigned a corresponding slotidx
CT = [0 0.5 0; 0.1 0.1 0.1; 0.5 0 0]; % Colours (percentage RGB scale,
% contains luminosity of colour), CT = Colour Table
nslots = numel(slotnums); % number of colour segments (also used to
% calculate the angle of rotation per frame)
SMAA=0:pi/20:2*pi; % pi/20 means there is 40 sides to the circle, smaller the
% increment smoother the ball
x=cos(SMAA); %X coordinate of each vertex
y=sin(SMAA); %Y coordinate of each vertex
k=5;% Radius of ball
ball(1,:) = k*x;
ball(2,:) = k*y-170; %y-axis is flipped when using imagesc, so to offset
% vertically upwards needs a negative y offset
landOn = find(slotnums == wNumber); %ball will land on the winning number
for i = 0:1:36
offset = i; % specify angle offset (degrees)
% Use meshgrid to define a coordinate plane
[x, y] = meshgrid(-220:220);
% Get polar coordinates of each point in the domain
[theta, rho] = cart2pol(x, y);
% generate segments
segm = mod(rad2deg(theta)+offset*30+90,360)/360; % normalize
segm = ceil(segm * nslots); % quantize (denormalize [1,37]), splits the
% segments into 37 inividually addressable coloured segments
segm = ind2rgb(segm,CT(slotColour,:)); %assigns rgb colour to each segment
% Colour table is generated from the ind2rgb command, uses values from
% slotColour (output is an MxNx3/RGB image)
% Define boundaries of the annular region
annular = double(rho >= rInner & rho <= rOuter);
segm(repmat(~annular,[1 1 3])) = 0; %Vector [1 1 3] creates a MxNx3 Mask
% second argument is number of times to replicate input for other
% dimensions, repmat(~annular) = 0 (disable/black out) would only affect
% the reds
% RGB Image components in terms of colour
% Reds = [: : 1], Greens = [: : 2], Blues = [: : 3]
% Additional comments: [1 1 0] = no segment is masked
% [1 1 1] = only reds are masked
% [1 1 2] = reds and greens are masked
% [1 1 3] = all segments are masked
% show result
imagesc(-220: 220, -220: 220, segm); %Displays image with x
% & y axis borders from -200 to 200, NOTE: changing scale of axis does
% not affect image itself, as the original coordinate plane is defined
% as meshgrid(-rOuter:rOuter);. However it will affect the plots below
axis off
axis square
hold on
%hold on
% add numbers
textrad = rInner + 0.65*(rOuter-rInner); % distance of the radius between
% the text and center of circle (Also the hypoteneuse)
th0 = 360/nslots; %Difference in angle between each segment/slot
theta = linspace(0,360-th0,nslots) - offset*30 + 275; %offset*30, for 3
% complete rotations, +275 to move number 0 into correct position
for k = 1:numel(theta) %Creates 37 numbers, as length of array elements is 37
x = textrad*cosd(theta(k)); %Pythagoras for x coordinate (adjacent
% length = hypoteneuse * cos(theta)
y = textrad*sind(theta(k)); %Pythagoras for y coordinate (opposite
% length = hypoteneuse * sin(theta)
numLabel = text(x,y,sprintf('%d',slotnums(k))); %converts number to
% double precision float (location of text is at [x,y])
numLabel.Color = [1 1 1];
numLabel.FontSize = 10;
numLabel.FontWeight = 'bold';
numLabel.HorizontalAlignment = 'center';
numLabel.Rotation = 270-theta(k);
end
% Drawing the ball
% For each iteration the angle changes by 2pi/n
comrotShape1 = rotateabout(ball, ((2*pi)/nslots * (i+landOn))+(5/360)*2*pi, 0, 0);
% 5/360*2*pi (converts offset angle of 5 degrees to rads) in clockwise
% direction as rotateabout uses clockwise rotation matrix
fill(comrotShape1(1,:),comrotShape1(2,:),[0.84 0.84 0.84]);
% Instantly plots arrow without waiting for further graphical commands
drawnow
hold off
pause(0.033)
end
function comrotShape = rotateabout(shape, a, p, q)
% Combined rotation and translation of shape, comrotShape short for combined
% rotation shape
%Tranlsate by (-p, -q)
Trans(1,:) = shape(1,:)-p;
Trans(2,:) = shape(2,:)-q;
%Rotate by a
rShape = [cos(a) -sin(a); sin(a) cos(a)] * Trans;
%Translate by (p, q)
comrotShape(1,:) = rShape(1,:)+p;
comrotShape(2,:) = rShape(2,:)+q;
end
end
7 Comments
Rik
on 6 Dec 2022
Why are you not using explicit handles everywhere for your graphics objects? That is considered best practice and helps when you want to delete elements.
David
on 6 Dec 2022
Walter Roberson
on 6 Dec 2022
ball = fill(comrotShape1(1,:),comrotShape1(2,:),[0.84 0.84 0.84]);
That is fine to record a copy of the handle(s) returned by fill.
David
on 6 Dec 2022
Walter Roberson
on 6 Dec 2022
You could
delete(findobj(gca,'type','text'))
but that risks deleting other text entries, and is not as efficient as what you are doing.
It looks to me as if you could probably vectorize that code. You can compute all of the coordinates in vector form, and you can text() arrays of coordinates and arrays of labels, passing in parameters such as 'Color', [1 1 1] that are in common for all of the labels. The one trick would be to set the rotations to be different for all of them you would have to do
set(TextHandles, {'Rotation'}, num2cell(270-theta(:)))
This relies upon an obscure way to use set() to change properties of multiple handles at the same time.
David
on 6 Dec 2022
Accepted Answer
More Answers (0)
Categories
Find more on Create Model Web Views 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!