How to run Main Function saved in mfile that has many subfunction that are saved in separate mfiles but all has to run together due to iteration within main function?
Show older comments
I am using R2016a version. I have a function file with a following structure -
function[out1, out2,...,out5]=MAINFUNCTION(inp1, inp2,inp3)
if nargin==2
statements of iteration
end
[lout1, lout2,]=LOCALFUN1(linp1,linp2)
[lout1, lout2,]=fmicon(@(X) LOCALFUN2(linp1,linp2),inpt6,)
[lout1, lout2,]=LOCALFUN2(linp1,linp2),inpt6,)
.
.
like this many subfunctions
.
.
end for MAIN FUNCTON
How to run this MAIN FUNCTION with all the sub functions saved in separate mfiles and can't be put in the main file due to optimisation restrictions put on them?
1. if I copy the entire function in command window i get error -Function definitions are not permitted in this context.
2. if I run from the RUN command the script I get error - Not enough input arguments.
what to do ?
19 Comments
The example code you have shown is not a script, but a function. Pressing RUN will not always work for functions, because you might need to provide inputs.
You can call your function from the command line:
[out1, out2,out3,out4,out5]=MAINFUNCTION(inp1, inp2,inp3)
"... this main function which has many local function embedded but saved in different scripts ?"
A local function by definition is saved in the same file as the main function of some Mfile. Local functions are only accessible to that main function and to any other local functions in the same Mfile.
"...can't be put in the main file due to optimisation restrictions put on them?"
What are those restrictions? I have never heard that MATLAB puts restrictions on local functions being put into the same Mfile as a main function.
What is this supposed to do?:
[lout1, lout2,]=LOCALFUN1(linp1,linp2)
[lout1, lout2,]=fmicon(@(X) LOCALFUN2(linp1,linp2),inpt6)
[lout1, lout2,]=LOCALFUN2(linp1,linp2),inpt6)
You simply discard and replace the results of each function, so at the end you will only have the results of the last function call. Is that what you want to achieve?
NS
on 16 Oct 2018
@NS: it is still not clear what you have, and what you are triying to do. Please answer these:
- How many functions do you want to call?
- Are they each saved in their own file?
- If not, how are they saved?
- Do you want to collect the outputs from calling each function, or do you want to process them straight away (e.g. maximum, sum, etc)?
- How are those "restrictions" defined? How do you want to apply them to your data?
NS
on 16 Oct 2018
Dennis
on 16 Oct 2018
if you have a function like this:
function myfunc(in1,in2,in3)
%code
end
I doubt that nargin (number of function input arguments) will ever be 4.
Dennis
on 16 Oct 2018
What happens if you call it with 4 inputs?
Stephen23
on 16 Oct 2018
Sir 1) How many functions do you want to call?
The function MainFinction has 3 local function inside it. I want to run the main function and each local function will generate certain output some of that will be used by next local function.
2. Are they each saved in their own file?
Yes sir each local function has it's own separate function file.
3.If not, how are they saved? Separate function mfile
4.Do you want to collect the outputs from calling each function, or do you want to process them straight away (e.g. maximum, sum, etc)?
Call each function that will generate some values I need but not all so the output argument mentions that for each local function.
How are those "restrictions" defined? How do you want to apply them to your data?
I am using optimset and fmincon to get minimum value of some GMM generalised method of moments function over some local function but intial random value is defined inside MainFinction.
@NS: your terminology is very very confusing. Please try to learn standard MATLAB terminology. If you want to communicate with other people then using a standard terminology helps because it means that other people will understand you. Your statements are confusing:
"The function MainFinction has 3 local function inside" To me, this would mean that you have one main function and three local functions saved in one Mfile.
"Yes sir each local function has it's own separate function file."
Then those are NOT local functions. Local functions are saved in the same Mfile as some main function. That is the definition of a local function in MATLAB.
NS
on 16 Oct 2018
Stephen23
on 16 Oct 2018
"...but all are being called inside the main functions. What do you call them subfunctions inside the main function?"
I think you answered your own question there: the place where a function is called from does not define the type of a function. The type of a function is solely determined by how and where it is defined, e.g.: the main function of an Mfile, local functions in the same Mfile, nested functions within any of those, anonymous functions can be defined anywhere:
But when a function is called it has no special name: functions can be called from the command line, or from a script, or inside a functions... but this makes no different to the type of the function: you are just calling a function (regardless of what type it is, or where it was defined). So as far as I can tell, you have three (more?) functions that you want to call. That is all.
NS
on 16 Oct 2018
NS
on 16 Oct 2018
"The reason I am unable to define 3 functions as Nested because these 3 functions do not have word function in their start within main function and do not have end when they finish."
If they do not start with function then they are not functions... and that would mean that they are scripts:
"Rather they are being referred inside main function as"
[lout1, lout2,]=LOCALFUN1(linp1,linp2)
Scripts do NOT have input and output arguments, so if you have scripts then this will not work. You can either convert them into functions by adding function .... end (recommended), or call the scripts without input/output arguments (not recommended).
NS
on 16 Oct 2018
"...BUT in Main function mfiles these subfunctions are called by the codes"
[lout1, lout2,]=SUBFUN1(linp1,linp2)
"and not starting with"
function SUBFUNC1
end
"How to run such functions?"
Just like your code already does, like this:
[lout1, lout2,]=SUBFUN1(linp1,linp2)
You do NOT define the function where it is called. When you need to call the function you just type its name, together with any input and output arguments... exactly as your code already does. This is explained in the MATLAB documentation, e.g. in the introductory tutorials, which explain how to call functions:
Are you having specific problems calling those functions? Do you get any warnings of error messages? If so, please show us the complete message.
Answers (0)
Categories
Find more on Matrix Indexing 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!