multiple function in one .m file

725 views (last 30 days)
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?

Accepted Answer

Adam
Adam on 2 Mar 2015
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 Comments
David Perez Ramos
David Perez Ramos on 2 Mar 2015
Thanks guys. I will use multiples files.
Stephen23
Stephen23 on 2 Mar 2015
Or you could check out my sadly too-late posted answer :)

Sign in to comment.

More Answers (3)

Stephen23
Stephen23 on 2 Mar 2015
Edited: Stephen23 on 13 Oct 2016
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 Comments
Venky Suriyanarayanan
Venky Suriyanarayanan on 31 Jul 2018
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 on 2 Nov 2020
we the people demand that this answer is accepted

Sign in to comment.


Steven Armour
Steven Armour on 13 Oct 2016
You can just switch to python or any of the other C-based languages; where this is not a problem
  2 Comments
Walter Roberson
Walter Roberson on 13 Oct 2016
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 on 20 Nov 2017
Edited: Moshe Flam on 20 Nov 2017
Maybe. But none of the millions of c programmers ever encountered that setting, let alone know about it. So the snide remark holds water.

Sign in to comment.


Cezar-Grigore Dihel
Cezar-Grigore Dihel on 25 Feb 2022
idk bro sorry

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!