how to change values from other script ?
21 次查看(过去 30 天)
显示 更早的评论
hello lets say i have a scripte named A( * the script A is function*) and in this script there is a value X=5 , and i have another script B, and i want to change the value of X in script B but also to change it at the same time at script A , how do i do it ? using global values
1 个评论
Stephen23
2017-10-25
"how do i do it ? using global values"
No, using global variables makes code buggy and hard to debug. Global variables are how some beginners write code that is impossible to fix.
Much simpler is to simply pass the value as an input/output argument, or use nested functions.
回答(3 个)
Jan
2017-10-25
编辑:Jan
2017-10-25
You do not change the value of a variable "in the same time" inside another script. Scripts share the workspace (list of variables) with their callers. Then a modification of a variable concerns all future processing of it in the caller of other scripts, which run afterwards. Consider, that calling a script is equivalent to inserting the contents of the script file in the caller. There are some differences in modern Matlab versions concerning locally defined function, e.g. in the /private subfolders, but the main idea is correct.
You can imagine that it has been severe drawbacks, if a modification in one script concerns all following code. Example:
% main.m
script1
script2
% script1.m
sum = 1:10
% script2.m
disp(sum(1:5))
If you now see script2 only, it seems strange, why it does not reply the sum, but treats "sum" as a variable.
Therefore scripts are not useful and reliable for productive code. Use functions instead to keep the workspace clean and to limit the effect of changes to the locally seen function. Global variables have similar drawbacks and it is a good programming practice to avoid using them. They impede the debugging and confuse the readers.
[EDITED] You have written in your comments, that one of the "scripts" is a function. Then use the standard inputs and outputs to share data with other functions. This is clean, reliable and efficient. It is a good programming practice from the view point of debugging, maintaining and expanding the code. You can e.g. edit the internal details of a function without any side-effects on other functions, as long as the inputs and outputs are kept.
Example:
function main
A = 1;
B = 2;
[A, B] = func1(A, B)
A = func2(A, B)
[A, B] = func1(A, B)
end
function [A, B] = func1(A, B)
A = A * 2;
B = A + B;
end
function A = func2(A)
A = -A;
end
Now func2 does not have an "immediate" effect to the value of A in func1. But the result is forwarded to the caller and available in func1 as soon as it is needed. This has the advantage, that you can track all changes of the contents of A. You can know exactly by seeing the code, where changes are made and where to fix the code in case of a bug. This is a massive advantage compared to scripts, global variables or the bad assignin methods.
KL
2017-10-25
Jan has explained it cleary but I'm just going to give you an example with functions.
Firstly, you need to know the difference between malab script and matlab functions. read this -> https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
Next, the difference between base and caller workspaces and how to share data between them. read these -> https://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html https://www.mathworks.com/help/matlab/matlab_prog/share-data-between-workspaces.html
Now let's say you create a matlab "function" called func_A,
x = func_A(a)
x=a;
end
then you create another "function" called func_B,
x = func_B(a)
x=a+10;
end
Now on the command line when you say,
func_A(5)
the value of x in func_A workspace is 5 and then the same is returned. Now, when you say,
func_B(5)
the value of x in func_B's workspace is 15, not 5! But in both cases you get the value of x returned by these functions and you can use this return value like,
func_A(func_B(5))
Now the value of x in both workspaces are 15, since you get the return value 15 from func_B and then pass it directly to func_A (where it's simply assigned to x).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!