Does anyone has some idea how to run this main function which has many local function embedded but saved in different scripts ?
Ahora está siguiendo esta pregunta
- Verá actualizaciones en las notificaciones de contenido en seguimiento.
- Podrá recibir correos electrónicos, en función de las preferencias de comunicación que haya establecido.
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?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
NS
el 15 de Oct. de 2018
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 comentarios
Dennis
el 16 de Oct. de 2018
Editada: Dennis
el 16 de Oct. de 2018
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)
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 2018
"... 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
el 16 de Oct. de 2018
Yes sir I am trying to calculate the results from three different restrictions put on some of my variable and the three local function give three different output . The optimisation will in end produces the results from only that local function that matches satisfies these restrictions.
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 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
el 16 de Oct. de 2018
Hi Denis I have tried that command but every time it stops at my first local function giving the error that not enough input argument as one of the input argument to my first local function is inside the Main function line
If nargin=4
D=10
Maxiter=15
End
If nargin =8
Maxiter=1
End
[out1,out2] = Local fun1(input1,D )
So basically it stops at hinting that it does not have D But D will only come when nargin=4 gets satisfied.
Dennis
el 16 de Oct. de 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.
NS
el 16 de Oct. de 2018
Editada: NS
el 16 de Oct. de 2018
Sir my main function has 8 input arguments but for the start the user gives 4 input argument and rest 4 are inside if loop like-
function [out1,out2] = Mainfun(in1,in2,in3,in4,Maxiter,dBest,initialval,val2)
If nargin=4
Maxiter=15
Dbest=10
Scale=100
End
If nargin=8
Maxiter=1
Dbest=1
end
[Out3,out4..]=localfun1(input10, Scale)
End
Stephen23
el 16 de Oct. de 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.
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 2018
@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
el 16 de Oct. de 2018
Editada: NS
el 16 de Oct. de 2018
If nargin is 4 then It will use the intial value as the random value that will be generated for all 15 iterations
If nargin is 8 they will use intial value as first random number values I have generated and doesn't have to perform any iteration as maxiter=1
NS
el 16 de Oct. de 2018
Sorry then the 3 sub functions are not local as they are saved in separate mfiles but all are being called inside the main functions. What do you call them subfunctions inside the main function?
Stephen23
el 16 de Oct. de 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
el 16 de Oct. de 2018
Thanks for the clarifications sir. I have 3 functions to call but there is 1 main function that performs iterations over them.
How to run this main function ?
NS
el 16 de Oct. de 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.
Rather they are being referred inside main function as
[lout1, lout2,]=LOCALFUN1(linp1,linp2)
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 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
el 16 de Oct. de 2018
No sir, apologies for my inability to explain to you my issue. What I meant was that these subfunctions are FUNCTIONs saved in their own respective mfiles starting with word function and ending with end 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?
Stephen23
el 16 de Oct. de 2018
Editada: Stephen23
el 16 de Oct. de 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.
Respuestas (0)
Ver también
Categorías
Más información sobre Software Development Tools en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Se ha producido un error
No se puede completar la acción debido a los cambios realizados en la página. Vuelva a cargar la página para ver el estado actualizado.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia-Pacífico
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)