Calling a Script within another script?

조회 수: 306 (최근 30일)
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 1월 23일
편집: Mario Malic 2021년 1월 23일
Since a similar question is posted on the forum, I have used the command run() as one of the members of the forum said, but It seems that it doesn't work here.
I am designing a code in a script that depending on the value of flag1 and flag2 values, it calls a script or the other one. This is the code:
flag1=0;
flag2=0;
problem='electric'; %this must be defined by the user
if problem=='electric'
flag1=1;
elseif problem=='mechanic'
flag2=1;
end
if flag1==1
run(ohm)
end
if flag2==1
run(programaultimate)
end
When I run this script, this is the error I get:
Depending on what the user puts in the "problem" line, the code will run the script called ohm or the script called programaultimate. The script I have posted, and those two scripts (ohm and programaultimate) are saved in the same folder. Also, I have tried calling them with the .m extension with an without the run command, and it doesn't work. What am I doing wrong? How can I achieve that?
Thank you very much

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2021년 1월 23일
This you can do this way:
if flag1 == 1
ohm
end
if flag2 == 1
programaultimate
end
HTH
  댓글 수: 1
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 1월 23일
Yes, I tried that and it didn't work. The reason for that is that I had a little error in the ohm script, that's why it didn't work. Now that I fixed the issue, this is the correct way of calling scripts within scripts.
Thank you!!

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

추가 답변 (1개)

Mario Malic
Mario Malic 2021년 1월 23일
편집: Mario Malic 2021년 1월 23일
Enclose your script name in quotation marks.
Edit: with the file extension.
run("ohm.m")
  댓글 수: 1
ErikJon Pérez Mardaras
ErikJon Pérez Mardaras 2021년 1월 23일
Thanks for your reply!
If I put run("ohm") this error appears:
And if I put only "ohm", this appears on the command window but it doesn't execute the script:

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by