필터 지우기
필터 지우기

Why, when running a script, matlab workspace shows variables do not present in the script?

조회 수: 19 (최근 30일)
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
Alessandro Cerrano
Alessandro Cerrano 2017년 10월 23일
편집: Alessandro Cerrano 2017년 10월 23일
Thank Jan for the answer, however I still do not realize why Matlab show in the workspace variables that are not present in the (only) script open in the editor.... In particoular the variables showed are referred to a funcion I found and used in another script (for time calculation), here the code
REPS = 1000; minTime = Inf; nsum = 10;
tic;
for i=1:REPS
tstart = tic;
sum = 0; for j=1:nsum, sum = sum + besselj(j,REPS); end
telapsed = toc(tstart);
minTime = min(telapsed,minTime);
end
averageTime = toc/REPS;
(this function is no more present in the actual script)
Jan
Jan 2017년 10월 23일
See [EDITED] in my answer. By the way: Please add comments to answers under the corresponding answer.

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

답변 (1개)

Jan
Jan 2017년 10월 23일
편집: Jan 2017년 10월 23일
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
Jolanda Müller
Jolanda Müller 2021년 4월 22일
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
Stephen23 2021년 4월 22일
"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.

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by