calling of functions and subfunctions

조회 수: 4 (최근 30일)
gb
gb 2019년 11월 12일
댓글: Walter Roberson 2019년 11월 13일
hi guys i am trying to make a switch case . however, the code i have involves a function calling other functions and so i cant create the switch. (when i create the switch and remove the function title for the subfunction, the main function cant run the case. for example:
function main
for ...
Y = EulerODE(tspan, Init,dt, Repressor, Vm(i), Kx(i));
...
...
...
...
end
...
...
Y = EulerODE2(tspan, Init,dt);
...
end
%subfunction 1
function Y = EulerODE(tspan,init,dt,Repressor,Vm,Kx)
...
end
%subfunction 2
function Y = EulerODE2(tspan,init,dt)
end
  댓글 수: 4
per isakson
per isakson 2019년 11월 12일
Is there an error message? If so show us the full error message.
I assume that your problem isn't caused by the switch-statement. What happens if you comment out the switch-statement?
"and memove the function title for the subfunction" what exactly does that mean?
gb
gb 2019년 11월 13일
i havent actually made a switch because what ive tried isnt working. what ive written above works fine. basically i want to change what ive written in my question to something like what ive written below but i believe i cant have a function inside the switch? :
function main
switch
case1
for ...
Y = EulerODE(tspan, Init,dt, Repressor, Vm(i), Kx(i));
...
...
...
...
end
function Y = EulerODE(tspan,init,dt,Repressor,Vm,Kx)
...
end
case 2
...
...
Y = EulerODE2(tspan, Init,dt);
...
function Y = EulerODE2(tspan,init,dt)
end
end %end switch
end %ends function main

댓글을 달려면 로그인하십시오.

채택된 답변

Cam Salzberger
Cam Salzberger 2019년 11월 12일
Hey Geoffrey,
Which version of MATLAB are you using? If you remove the main "function" declaration, it will consider the file to be a script. Local functions in scripts only started being supported in R2016b, so if you have a version earlier than that, then MATLAB will error. If you would prefer to use a script, you'll need to make separate function files for each of your local functions.
-Cam
  댓글 수: 2
gb
gb 2019년 11월 13일
편집: gb 2019년 11월 13일
im using the online version of matlab, i have also the R2019b version. i havent actually made a switch because what ive tried isnt working. basically i want to change what ive written in my question to something like what ive written below but believe i cant have a function inside the switch? :
function main
switch
case1
for ...
Y = EulerODE(tspan, Init,dt, Repressor, Vm(i), Kx(i));
...
...
...
...
end
function Y = EulerODE(tspan,init,dt,Repressor,Vm,Kx)
...
end
case 2
...
...
Y = EulerODE2(tspan, Init,dt);
...
function Y = EulerODE2(tspan,init,dt)
end
end %end switch
end %ends function main
Walter Roberson
Walter Roberson 2019년 11월 13일
You are correct that functions cannot be defined inside control statements such as switch or if.
You can move all of the nested function definitions to just inside the "end" statement of the function.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by