how to change values from other script ?

18 次查看(过去 30 天)
tomer polsky
tomer polsky 2017-10-25
评论: Jan 2017-10-27
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
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
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.
  1 个评论
KL
KL 2017-10-25
Jan, You have a typo there. Probably you may need another coffee:)

请先登录,再进行评论。


KL
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
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).

tomer polsky
tomer polsky 2017-10-26
编辑:tomer polsky 2017-10-26
first of all thank each of you people for answering me . second i am new at matlab, so i will show the you the codes that i wrote :
{
function [x_diff]=finding_x_state_varbile(t,x)
for values_of_circuit=0
global R R1 C C1 L1 L2 U
U=12;
R=12.5;
R1=0.5;
C=2200e-6;
L1=1e-3;
L2=1e-3;
C1=470e-6;
end
for basic_matrix=1
A_a=[0 0 0 1/(C1);0 -1/(R*C) 0 0;0 0 -R1/(L1) 0;-1/(L2) 0 0 0];
B_a=[0;0;(1/L1);0];
A_b=[0 0 1/C1 0;0 -1/R*C 1/C -1/C;-1/L1 -1/L1 -(R1)/L1 0;0 1/L2 0 0];
B_b=[0; 0 ;1/L1; 0];
C_a=[0 0 1 0 ];
C_b=[0 1 0 0];
end
for Duty_cycle=2
Duty_cycle=70; alpha=(1-Duty_cycle)/Duty_cycle;
end for avrage_of_basic_matrix=3 A_T=(A_a+alpha*A_b)/(1+alpha); B_T=(B_a+alpha*B_b)/(1+alpha); C_T=(C_a+alpha*C_b)/(1+alpha); D_T=0; end x_diff=A_T*x+B_T*U; end }
and then the second function is : {
global R R1 C C1 L1 L2 U
U=48;
R=12.5;
R1=0.5;
C=2200e-6;
L1=1e-3;
L2=1e-3;
C1=470e-6;
tspan=[0 0.18]; x0=[0 0 0 0]; [t,x]=ode23t(@finding_x_state_varbile,tspan,x0); figure(1) plot(t,x); }
and i want to change for exmaple U and that it will change in
{
function [x_diff]=finding_x_state_varbile(t,x)
}

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by