Does anyone has some idea how to run this main function which has many local function embedded but saved in different scripts ?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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?
조회 수: 1 (최근 30일)
이전 댓글 표시
NS
2018년 10월 15일
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
Dennis
2018년 10월 16일
편집: Dennis
2018년 10월 16일
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
2018년 10월 16일
편집: Stephen23
2018년 10월 16일
"... 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
2018년 10월 16일
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
2018년 10월 16일
편집: Stephen23
2018년 10월 16일
@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
2018년 10월 16일
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
2018년 10월 16일
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
2018년 10월 16일
편집: NS
2018년 10월 16일
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
2018년 10월 16일
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
2018년 10월 16일
편집: Stephen23
2018년 10월 16일
@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
2018년 10월 16일
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
2018년 10월 16일
"...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
2018년 10월 16일
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
2018년 10월 16일
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
2018년 10월 16일
편집: Stephen23
2018년 10월 16일
"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
2018년 10월 16일
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
2018년 10월 16일
편집: Stephen23
2018년 10월 16일
"...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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)