When I run the code of area.m it says'Attempt to reference field of non-structure array.'

The code for area.m is
radius = input('Enter the value of the radius');
[ a c ] = myfunc(radius);
fprintf('For the circle with radius of %0.1f ,\n',radius);
fprintf('the area is %0.1f and the circumfrence is %0.1f,\n',a,c);
The code for myfunc is
function [a c] = myfunc(radius)
a = 3.14 * radius* radius;
c = 2*3.14*radius;
Looking for a quick reply,
Thanks

Answers (2)

area.m is a MATLAB function, so you should not name your script area
rename your script circarea.m.
Then make sure you save your function myfunc.m in a folder that is on the MATLAB path. For example, if your folder name is c:\mfiles, you can add this path with
>>addpath 'c:\mfiles'
or by using the pathtool
>>pathtool
Also, FYI. MATLAB understands pi:
function [a c] = myfunc(radius)
a = pi*radius^2;
c = 2*pi*radius;
end
In addition to what Wayne says:
That particular error message is typical when people use the ".m" extension when they evaluate a function or script:
area.m %WRONG
area %right

This question is closed.

Tags

Asked:

on 12 Aug 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!