![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240306/image.png)
Plotting figures with user defined functions
4 views (last 30 days)
Show older comments
I'm trying to plot an image of a train sketch using a rectangle function and a circle function but I don't understand why it's not giving the right output.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240305/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240305/image.png)
Here's my code:
plotrectangle(1, 1.5, 3, 2)
hold all;
plotrectangle(3, 3.5, .5, .5)
plotcircle(2.5,4,1)
plotcircle(1.5, 4.5, 2)
plotcircle(1.5,1,2)
plotcircle(3.5,1,2)
function [] = plotrectangle(x, y, l, w)
figure;
rectangle('Position', [x y l w]);
axis( [0 10 0 10] )
end
function [] = plotcircle(c1,c2,r)
t = 0:0.0001:2*pi;
x = r*cos(t)+c1;
y = r*sin(t)+c2;
plot(x, y);
axis( [0 5 0 5 ] )
end
0 Comments
Answers (1)
Matt J
on 28 Sep 2019
Edited: Matt J
on 28 Sep 2019
For some reason, you've given circle radii that are all off by a factor of 6.
plotrectangle(1, 1.5, 3, 2)
hold on
plotrectangle(3, 3.5, .5, .5)
plotcircle(2.5,4.5,1)
plotcircle(1.5, 4.5, 2)
plotcircle(1.5,1,3)
plotcircle(3.5,1,3)
hold off
axis equal
function [] = plotrectangle(x, y, l, w)
rectangle('Position', [x y l w]);
end
function [] = plotcircle(c1,c2,r)
r=r/6;
t = 0:0.0001:2*pi;
x = r*cos(t)+c1;
y = r*sin(t)+c2;
plot(x, y);
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240306/image.png)
0 Comments
See Also
Categories
Find more on Annotations 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!