Calling a script within another script
92 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sanjit Ganti
am 8 Mär. 2017
Kommentiert: Sanjit Ganti
am 9 Mär. 2017
I am trying to call a script (say script2) within another script (say script1). But I have to declare a few variables in script1 before calling script2. When I try to do this, the script1 directly goes to calling script2, without declaring the variables in between. How to overcome this?
{clear
xq=2; yq=2;
script2}
4 Kommentare
Jan
am 8 Mär. 2017
@Sanjit: Please do not invent an own syntax containing curly braces to explain code in the forum. Prefer standard Matlab syntax, because this is understood here in general.
Akzeptierte Antwort
Jan
am 8 Mär. 2017
Bearbeitet: Jan
am 8 Mär. 2017
You can call one script from inside another without any problems:
% Script 1:
disp('I am script 1')
clear
a = 1;
b = 2;
Script2;
disp('Back in script 1')
disp(a)
disp(c)
And the second script:
% Script2
disp('I am script 2')
c = a + b
When you observe something like "script1 directly goes to calling script2, without declaring the variables in between", there must be another problem. Either you have not saved the script, or you run another script than you think you do. Use the debugger to find out the details: Set a breakpoint in the first line of the script and step through the code line by line.
If the 2nd script contains a clear, all formerly declared variables are deleted. It is a good prgramming pratice to avoid scritps and use functions instead. This allows to control the input and output of variables accurately and without any side-effects. Then the clear command can be omitted completely.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Scope Variables and Generate Names finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!