Good day.
I have the following function and I can't seem to be able to fix the error. This is a function that was in a pdf file I'm learning from and it told me to put it into matlab to see how it works and I'm pretty sure it was not supposed to do what it has done. This is NOT for an assignment or any other important thing that requires my own working, just so your concense is eased. You might it suspiciouse that I state this but I have been asked this question before so I answered it ahead of time.
Here is the function so you can copy it into your own Matlab.
function A = circle_area(diameter)
% help comments
% This function calculates the area of a
% circle. [Output units] = [Input units]^2
% Area = circle_area(diameter)
r = diameter/2;
A = pi*r.^2;
A = circle_area(0.2)

 채택된 답변

Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일

0 개 추천

Have I named it correctly?

댓글 수: 5

KSSV
KSSV 2020년 6월 11일
Rename Circle_area_diameter to circle_area.m. Save it as the name of the function.
Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일
Thanks alot.
KSSV
KSSV 2020년 6월 11일
Dude ....you have to accept my answer.....
Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일
I think I did. Does it not show up on your side? Because I did accept it on this side and I checked to see if I missed any un-accepted answers
KSSV
KSSV 2020년 6월 11일
편집: KSSV 2020년 6월 11일
Actually, this is like your answer.....I was answering from my mobile due to network problem and didn't realize that you have made your comment at answer section.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Goodmanson
David Goodmanson 2020년 6월 11일
편집: David Goodmanson 2020년 6월 11일

0 개 추천

Hi Morne'
Here Matlab assumed that the call to the function was part of the function itself. In a script, functions must be located below the main body of the code and each function must end with 'end'
A = circle_area(0.2)
function A = circle_area(diameter)
% help comments
% This function calculates the area of a
% circle. [Output units] = [Input units]^2
% Area = circle_area(diameter)
r = diameter/2;
A = pi*r.^2;
end

댓글 수: 1

Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일
Thanks Mr Goodmanson, I treid this and it didn't work so I'll do what you suggested alond with what KSSV told me below.

댓글을 달려면 로그인하십시오.

KSSV
KSSV 2020년 6월 11일

0 개 추천

I suspect, you tried running the function using f5 button or run button from the editor. It is not the way to call the function. Copy this function in a folder and go to that folder, make it present working folder. And in the work space type this.
diameter = 5. ;
A = circle_area(diameter) ;
Now you will get the value of A.

댓글 수: 4

Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일
By workspace do you mean the command window because I can't type directly into the workspace area?
KSSV
KSSV 2020년 6월 11일
Yes...command window...:)
Morne' Breitenbach
Morne' Breitenbach 2020년 6월 11일
So I made it my current working folder and I got this.
Sorry about how the image looks I can't seem to resize it.
KSSV
KSSV 2020년 6월 11일
You have named your function different. Name of the function and file name should be same.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2020년 6월 11일

편집:

2020년 6월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by