How to replace a pointer or marker with an object for tracing the desired path?

figure;
hold on
a=plot([10 10 5 5],[0 10 10 15],'r')
xlim([0 20])
ylim([0 20])
I want an object defined as blue rectangle with dimensions 0.5m*1m to trace the path given in the code.
How to do ?

 Accepted Answer

Use plot(), rectangle(), and pause(). Try this:
x = [10 10 5 5];
y = [0 10 10 15];
a=plot(x, y, 'r', 'LineWidth', 3)
xlim([0 20])
ylim([0 20])
xticks(0:20);
yticks(0:20);
grid on
hold on;
axis equal
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
yHeight = 0.5;
xWidth = 1;
for k = 1 : length(x)
xBoxLeft = x(k) - xWidth/2;
yBoxBottom = y(k) - yHeight/2;
hRect = rectangle('Position', [xBoxLeft, yBoxBottom, xWidth, yHeight], 'LineWidth', 2, 'EdgeColor', 'b');
pause(1);
delete(hRect);
end

9 Comments

here it only jumps to the coordinates, without tracing the entire path.i i also want to fill the blue colour within the object.
Then you need to give additional coordinates to fill in the "in between" points. Instead of just 4 coordinates, use like a hundred or something. i trust you can do that if you use linspace() - look it up in the help if you're not familiar with it. It's easy to use so I'm sure you can do it.
You can use the 'FaceColor, 'b' to get filled blue.
It is not possible for the code to trace the entire path. There is an infinity of points between any two coordinates, but the coordinates of any point that MATLAB can draw only has finite resolution.
MATLAB only renders at single precision. If we consider moving from (10,10) to (5,10), that would require moving from single(10.0) to single(5.0). I calculate that would require 8388608 steps in single precision. At 100 frames per second, that would require 23 hours 18 minutes. But first it would have had to move from (10,0) to (10,10), which I calculate would take over 34 years at 100 frames per second.
You may wish to reconsider not allowing the plotting to jump between coordinates.
tried, suggest corrections as the code doesnot yield the required output
x8d=linspace(150,98,180)
y8d=linspace(69,69,180)
%r6=24
xc8 = 75;
yc8 = 69;
r1=23
theta11 = linspace(0,-pi,180);
x8c = r1*cos(theta11) + xc8;
y8c = r1*sin(theta11) + yc8;
x8b=linspace(52,0,180)
y8b=linspace(69,69,180)
x888=[ x8d x8c x8b];
y888=[ y8d y8c y8b];
axis([0 50 0 50])
for k1 = 1:540
r=rectangle('Position', [x888(k1) y888(k1) 0.5 1], 'FaceColor',[0 0 1])
set(r, 'Position',[x888(k1) y888(k1) 0.5 1]);
drawnow
end
axis([0 150 0 70])
and remember to add a semi-colon at the end of the "rectangle" call. Also you do not need the set() call.
Thank you so much for your guidance
why is this error shown whenever i run the code "While setting property 'Position' of class 'Rectangle': Value must be numeric"
You forgot to include your complete error message, so we can't see what string or other variable you're putting into the array. It must be numbers, not strings, structures, etc. Please show ALL THE RED TEXT not just part of it. You left out the crucial part - your line of code!
the error message :
Error using rectangle While setting property 'Position' of class 'Rectangle': Value must be numeric
Error in pointer14 (line 30) r=rectangle('Position', [x888(k1) y888(k1) 1 3], 'FaceColor','r');
(For the code mentioned in the comments)

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Tags

Asked:

h b
on 23 Jan 2017

Edited:

h b
on 25 Jan 2017

Community Treasure Hunt

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

Start Hunting!