multiple function in one .m file

725 ビュー (過去 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 Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by