MATLAB Answers

Can I save multiple seperate functions to one .m file?

269 views (last 30 days)
Philip Brewer
Philip Brewer on 15 Feb 2018
Commented: Walter Roberson on 28 Jan 2020
I see that when I write a function that it can only be named the same as the .m file name, is there any way that I can change that so that I can call on the function by a different name? I want to do this so that I can put two different functions in one file. For example I have to functions and I want them to be:
function mult(a,b)
c=a*b
end
function div(a*b)
d=a/b
end
I want to call on these functions at separate times, not run them together, can I have them both in the same file?

  7 Comments

Show 4 older comments
Walter Roberson
Walter Roberson on 15 Feb 2018
No, not correct. I explained below two of the ways it is possible to call such functions.
What is not possible is to put a buch of function in the same file and tell MATLAB to use the file as a library. Only the first function in a file is immediately reachable by simple unqualified function name, except for whatever you can do with classdef
Philip Brewer
Philip Brewer on 15 Feb 2018
Thanks, this is what I was trying to do
function ideal(R)
n=2;
V=1;
P=100:10:400;
igl=(P*V)/(n*R);
T=table(P', igl');
T.Properties.VariableNames = {'Pressure_Bar' 'Ideal_temp_K'}
end
function waals(R,a,b)
n=2;
V=1;
P=100:10:400;
E1=P+(n^2*a/V^2);
E2=V-n*b;
E3=E1*E2;
E4=n*R;
vdw=E3/E4;
T=table(P', vdw');
T.Properties.VariableNames = {'Pressure_Bar' 'Waal_temp_K'}
end
But from what I am seeing, both of these functions would need to be in separate files.
Walter Roberson
Walter Roberson on 15 Feb 2018
Those would need to be in separate files.
You should probably be returning T from the function.

Sign in to comment.

Answers (3)

per isakson
per isakson on 28 Jan 2020
Edited: per isakson on 28 Jan 2020
IMO: The best solutions is a class with statics methods.
>> lib.div( 8, 2 )
ans =
4
>> lib.mult( 8, 2 )
ans =
16
where
classdef lib
methods ( Static = true )
function c = mult(a,b)
c=a*b;
end
function d = div(a,b)
d=a/b;
end
end
end

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 15 Feb 2018
No. Only the first function in a .m file can be invoked by name, and the name it is known by to be called is the same as the name of the .m file rather than the actual function name (but modern versions will give you a warning if those are not the same.)
If you need to be able to invoke a different function, then the outside world has to somehow be given a handle to the function. For example,
function funs = switchyard
funs.mult = @mult;
funs.div = @div;
end
function c = mult(a, b)
c = a*b;
end
function d = div(a, b)
d = a/b;
end
then it would be legal for something to call
F = switchyard();
F.div( F.mult(3, 7), 19)
The closest that there is to an exception to this is class definitions, which you to create object-oriented objects and put several implementing functions into the same .m file.
You should probably also have a look at "packages" . And at "private" directories.

  2 Comments

Stephen Cobeldick
Stephen Cobeldick on 15 Feb 2018
Just to emphasize that while it is certainly possible to pass handles and use them, in most cases it would be easier to keep the Mfiles simple and use a private directory, or to write class.
Walter Roberson
Walter Roberson on 15 Feb 2018
switchyards were not uncommon in earlier MATLAB when objects were implemented as struct (struct are still fairly important to objects under the hood but the implementation is significantly different now.)
The Instrument Control toolbox effectively constructs the kind of struct I show, holding pointers to the implementing functions for the interface associated with the object being constructed.

Sign in to comment.


Sidra  Ashraf
Sidra Ashraf on 28 Jan 2020
addpath([functiondir 'AAM Functions'])
addpath([functiondir 'Functions'])
AAM Functions and Functions both are files in which multiple functions are existing.
now the question is that where these should be placed in the same directory or separete??

  1 Comment

Walter Roberson
Walter Roberson on 28 Jan 2020
Are those files or are they directories? addpath() can only be used with directories. If they are directories then MATLAB is able to find code in either of them when they are both on the path, so use two or use a combined one depending on the software engineering needs. (Methods defined in private directories can only be called from the parent directory, so if you had functions in AAM Functions that needed to call something in Functions/private then you would need to combine the directories.)

Sign in to comment.

Sign in to answer this question.


Translated by