Why, when running a script, matlab workspace shows variables do not present in the script?
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I'm a newbie with Matlab, I can't understand why my workspace shows variables do not present in the script, and this create errors, e.i I can not do a summation because when I ask which sum matlab return that sum is a variable, when sum is not actually initialized in the script....
3 Kommentare
Alessandro Cerrano
am 23 Okt. 2017
Bearbeitet: Alessandro Cerrano
am 23 Okt. 2017
Jan
am 23 Okt. 2017
See [EDITED] in my answer. By the way: Please add comments to answers under the corresponding answer.
Antworten (1)
Jan
am 23 Okt. 2017
Bearbeitet: Jan
am 23 Okt. 2017
Please read in the documentation to learn the difference between scripts and functions. Scripts share the workspace of the caller, while each function has its own workspace. The described effects are wanted and the purpose of scripts. If you want your workspaces clean and separated from each other, use functions.
You find many threads in this forum concerning the redefinition of "sum" as a variable and the later failing of calling this function. A solution is:
clear sum
But a much more better approach is to avoid the shadowing of built-in functions in general.
[EDITED after your comment]
Any variable, which is created in a script, is shared with the caller. And if you call the next script, the variables are forwarded again.
I do not know what "(this function is no more present in the actual script)" means: Which function, what is "no more present" and which one is the actual script?
Scripts are equivalent to copying their code to the calling functions. Example:
% script1.m % Stored in separate file
a = 5
% script2.m % Stored in separate file
b = 3
c = b + a
% Main.m % Stored in separate file
script1
script2
disp(a)
disp(b)
disp(c)
This is equivalent to inserting the code of the scripts inside the main function:
% Main2.m % Stored in one file
a = 5 % from script1
b = 3 % from script2
c = b + a
disp(a)
disp(b)
disp(c)
Does it getting clear now? If not, did you really take the time to search in the documentation fro the corresponding explanations? See https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html . It is explained clearly and exhaustively there.
4 Kommentare
Jolanda Müller
am 22 Apr. 2021
Thank you for your answer. In this example, you say that creating the variables with a script that is called in the function or creating them directly inside the function are equivalent. (This is exactely what I also thought was the case.)
However, I ran into a problem recently: If the variable that is created inside of the called script shares the name of something else (like another script in the current folder or a matlab inbuilt function), the calling function fails to use the variable that is assigned in its workspace. If I create the values directly in the function (not via calling a script) this issue does not happen.
This sounds a bit confusing, so I will try to be more clear using an example:
VERSION 1
If I call this version of myFunction() everything works fine, and the output is 2:
function [diff] = myFunction()
sum = 5;
test = 3;
diff = sum - test;
end
VERSION 2
If I call this version of myFunction()
function [diff] = myFunction()
run('script1.m')
run('script2.m')
run('script3.m')
disp(completelyUniqueName);
diff = sum - test;
end
% script1.m
sum = 5;
%script2.m
test = 3;
%script3.m
completelyUniqueName = 10;
then matlab tries to use the inbuilt function "sum" inside myFunction, eventhough the variable "sum" is in the workspace of the function and throws an error and says
Error using sum
Not enough input arguments.
If I rename "sum" to something that is not overwriting an inbuilt function, it works fine. It is still annoying, because it might happen accidentally anyways (for example the name "material" cannot be used, because apparently it is already used by matlab for something, which I didn't know or care about), and I would not expect it to behave that way.
Additionally, and almost worse than the issue with inbuilt functions is this: If in my above example, I have a script called "test.m" somewhere in my active folder (which could very easily happen), then myFunction will try to run the script "test.m" instead of using the variable "test" that has been created in myFunction's workspace by script2.m.
Sidenote: "completelyUniqueName" does not cause any porblems in the example, since it's name is neither used for a matlab inbuilt function or for another script.
Why does this happen? And how can it be avoided? (In my situation it is quite curcial, that these variables are created by a separate script and not in the function).
Stephen23
am 22 Apr. 2021
"Why does this happen?"
The JIT compiler finds the existing function and optimizes your code using that.
"And how can it be avoided?"
- best solution by far: write functions instead of scripts.
- declare variables in the calling workspace.
- use unique names.
Siehe auch
Kategorien
Mehr zu Debugging and Analysis 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!