multiple function in one .m file

841 次查看(过去 30 天)
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
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
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
idk bro sorry

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by