How to do multiple functions or subroutines in one .m file

조회 수: 72 (최근 30일)
Collin Greenwell
Collin Greenwell 2021년 8월 25일
댓글: David Goodmanson 2022년 7월 19일
I am working on a project for a class and the professor is requiring that we use functions for evaluating certain things (using the bisection and secant method), but he also requires that we only turn in a single .m file. Normally I would create the functions in seperate files and then call them in the main code, but he does not want this. He insists there is a way to do multiple "subroutines" (his phrasing, although I havent heard of that and when I look it up it seems to be the same thing as a function. The main problem is if I try and put a function into my main project code named project1.m it says that the function (bisect) is not the same as the file name, which is why I've always done them seperately. I have looked throughout these forums and the methods I've seen dont seem to make much sense. Ill put a shortened version of my code, as the guts of it don't matter since they run fine. Overall i need a way to have two funcitons or "subroutines" in one .m file and be able to call them. The main problem is having them in the same file and using them all in the same script.
function [p,i] = bisect(f,a,b,tol,n0)
% code for evaluating the function
end
[p,i] = bisect(f,a,b,tol,n0);
disp(p);
disp(i);
  댓글 수: 1
Stephen23
Stephen23 2021년 8월 25일
편집: Stephen23 2021년 8월 25일
"He insists there is a way to do multiple "subroutines" (his phrasing, although I havent heard of that and when I look it up it seems to be the same thing as a function."
They are called local functions:
"The main problem is if I try and put a function into my main project code named project1.m it says that the function (bisect) is not the same as the file name, which is why I've always done them seperately."
If you are defining functions in a script then all function definitions must occur after all script code.
This is explained in the MATLAB documentation:

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

답변 (1개)

David Goodmanson
David Goodmanson 2021년 8월 25일
Hi Collin
Here is a quick example. Functions must go at the very end of the script, and each function must have an 'end' statement at the end to delineate it from the next function.
r = 1;
s = 2;
u = 0:.01:6;
x = afun(r,u);
y = bfun(s,u);
figure(1)
plot(x,y)
grid on
function x = afun(r,u)
x = r*cos(u);
end
function y = bfun(s,u)
y = s*sin(u);
end
  댓글 수: 2
Warwick
Warwick 2022년 7월 17일
This is by far the best answer to this question. I had the same question but the offical help and other user answers regarding local functions were, to me at least, ambiguous. thanks.
David Goodmanson
David Goodmanson 2022년 7월 19일
Hi Warwick, thanks for your comment. Good to know that the answer was useful.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by