multiple function in one .m file

조회 수: 254 (최근 30일)
David Perez Ramos
David Perez Ramos 2015년 3월 2일
답변: Cezar-Grigore Dihel 2022년 2월 25일
Hello, I am trying to input multiple function in my assignment using integral with the trapz command. This is one of my functions:
EDITOR:
function z= myfun2(t)
z(1) = 10*t;
end
COMMAND WINDOW:
t = linspace(0,1,400);
z = myfun(t)
area = trapz(t,z)
Now, I have multiple functions to input with different values of t. how do I put them all in one .m file on the EDITOR for expample:
function z= myfun2(t)
z(1) = 10*t; the value of to here will be t= linspace (0,1,400);
z(2) = 10*t.^2; the value of to here will be t= linspace (-1,2,400);
z(3) = 5*exp.^(-2*t); the value of to here will be t= linspace (-1,1,400);
end
How can I set this in one .m file with different values of t and obtain the results in my COMMAND WINDOW for all of them?

채택된 답변

Adam
Adam 2015년 3월 2일
You cannot define more than one function in a file to have external access. A function visible from the command line must share the name of the file it is saved in, hence only one can be thus defined. You can define as many as you like within the file that have only file scope - i.e. they can be called from the main function in the file or from each other, but otherwise you need a file for each function.
Alternatively you can use a class, but unless you are familiar with Matlab OOP that is an un-necessary complication and even if you do use OOP it is not necessarily a better solution than just having one per file.
  댓글 수: 3
David Perez Ramos
David Perez Ramos 2015년 3월 2일
Thanks guys. I will use multiples files.
Stephen23
Stephen23 2015년 3월 2일
Or you could check out my sadly too-late posted answer :)

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

추가 답변 (3개)

Stephen23
Stephen23 2015년 3월 2일
편집: Stephen23 2016년 10월 13일
The first function shows a basic misunderstanding of MATLAB's indexing and arrays, which has nothing to do with functions per se. Lets have a look:
function z= myfun(t)
z(1) = 10*t;
end
is called from the command window with this (after fixing the incorrect function name):
>> t = linspace(0,1,400);
>> z = myfun(t)
However this immediately produces an error, proving that you did not even try the code that you posted:
In an assignment A(I) = B, the number of elements in B and I must be the same.
What does this mean? This is because t is a vector of values (it has 400 elements!), whereas inside myyfun you are string to put 400 elements in one element z(1) with this allocation:
z(1) = ... 400 elements
This will always be an error in MATLAB: every element must have its own position. One solution is to use some indexing , but the best options is to simply ignore the indexing altogether:
z = ... 400 elements
This will then work without error.
But how can we fix the second function? In this case you actually want to use different t values on each function. There are many solutions to this, here are two that you could try:
1) an easy but not very good way would be to return one numeric matrix for each call of the function, with all values inside:
function z= myfun2(t)
z(1,:) = 10*t;
z(2,:) = 10*t.^2;
z(3,:) = 5*exp(-2*t); % fixed syntax mistake with exp.
end
2) a much better solution is to return function handles for each function, and evaluate these in the command window:
function fun = myfun2
fun{1} = @(t) 10*t;
fun{2} = @(t) 10*t.^2;
fun{3} = @(t) 5*exp(-2*t); fixed syntax mistake with exp
end
calling this function will return a cell array of functions handles : these can then be evaluated with whatever t values you want:
>> fun = myfun2();
>> y = fun{2}(t) % evaluates the second function
  댓글 수: 3
Venky Suriyanarayanan
Venky Suriyanarayanan 2018년 7월 31일
Fantastic answer..!! Both the methods work like a charm though I eventually ended up using method (1) because it suited my project requirements better. Thanks a lot for taking out time and explaining in such great details.
P Lepage
P Lepage 2020년 11월 2일
we the people demand that this answer is accepted

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


Steven Armour
Steven Armour 2016년 10월 13일
You can just switch to python or any of the other C-based languages; where this is not a problem
  댓글 수: 2
Walter Roberson
Walter Roberson 2016년 10월 13일
Your remark is not correct. The external visibility of multiple functions in a single source code in C is controlled by the linker, the workings of which is outside the C standard. Linkers can have complicated rules about visibility, often requiring that control files be built to describe the visibility; the same control file might or might not also be used to control address layouts or absolute addresses that items need to be linked at. It is a problem.
Moshe Flam
Moshe Flam 2017년 11월 20일
편집: Moshe Flam 2017년 11월 20일
Maybe. But none of the millions of c programmers ever encountered that setting, let alone know about it. So the snide remark holds water.

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


Cezar-Grigore Dihel
Cezar-Grigore Dihel 2022년 2월 25일
idk bro sorry

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by