MATLAB Answers

Writing Functions that take inputs?

14 views (last 30 days)
Tyler Bodnarik
Tyler Bodnarik on 30 Sep 2020 at 2:45
Edited: Stephen Cobeldick on 30 Sep 2020 at 8:35
Need help getting this function to execute.
So far I have:
r = input('What is the radius of the circle?')
x = input('What is the X - coordinate for the center of the circle?')
y = input('What is the Y - coordinate for the center of the circle?')
c = 'k';
function circleplot(x,y,r,c)
hold on
th = 0:pi/50:2*pi;
x_circle = r * cos(th) + x;
y_circle = r * sin(th) + y;
plot(x_circle, y_circle);
fill(x_circle, y_circle, c)
plot(x, y, 'kp', 'MarkerSize',15, 'MarkerFaceColor','k')
axis equal
end
When I run the code nothing happens expect the prompts asking me for data. Any advice?

  0 Comments

Sign in to comment.

Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 30 Sep 2020 at 7:33
Edited: Stephen Cobeldick on 30 Sep 2020 at 8:35
"When I run the code nothing happens expect the prompts asking me for data."
You defined a function in a script (which is permitted syntax since R2016b) but you do not actually call the function anywhere. If you do not call a function it does not run: https://www.mathworks.com/help/matlab/learn_matlab/calling-functions.html
Here are two simple solutions to fix your code:
One: actually call the function in your script:
r = input('What is the radius of the circle?')
x = input('What is the X - coordinate for the center of the circle?')
y = input('What is the Y - coordinate for the center of the circle?')
c = 'k';
circleplot(x,y,r,c) % <- call it here !
function circleplot(x,y,r,c)
...
end
Two: get rid of the function, turn it into a simple script:
r = input('What is the radius of the circle?')
x = input('What is the X - coordinate for the center of the circle?')
y = input('What is the Y - coordinate for the center of the circle?')
c = 'k';
hold on
th = 0:pi/50:2*pi;
x_circle = r * cos(th) + x;
y_circle = r * sin(th) + y;
plot(x_circle, y_circle);
fill(x_circle, y_circle, c)
plot(x, y, 'kp', 'MarkerSize',15, 'MarkerFaceColor','k')
axis equal

  0 Comments

Sign in to comment.


Steven Lord
Steven Lord on 30 Sep 2020 at 3:09
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by