How to use a variable from a script in another script
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Isaac Malceod
am 15 Mai 2019
Kommentiert: Adam Danz
am 17 Mai 2019
I am trying to write a script that demonstrates the methods of finding roots in graphs (regula falsi, bisection and newton raphson) and want to demonstrate the number of steps each one took at the end. Currently i am trying to do this by creating a variable of the number of steps at the end of the functions for example my for regula falsi:
function root = regula_falsi(a,b)
% implementation of the regula falsi method starting
% with x=a and x=b as the brackets
% check that the two points bracket the root
if(~(f(a)*f(b)<0.0))
disp('Root not bracketed!');
return;
end
% set the tolerance: how close to the root
% does the function have to be?
tol = 1.0e-9;
% keep count of the number of steps
step = 1;
while(abs(f(a))>tol && abs(f(b))>tol)
% print out for use
fprintf('step %d, a = %d, b = %d\n',step,a,b)
% find midpoint
m = a - (b-a)/(f(b)-f(a))*f(a);
% which are the new brackets?
if(~(f(a)*f(m)<0.0))
a=m;
else
b=m;
end
step = step + 1;
end
if a==m
fprintf('root is at %d',a)
else
fprintf('root is at %d',b)
end
c=step
however when i try to use the variable c in another script it is not defined so how could i change this so that the variable c is in my workspace
4 Kommentare
Bob Thompson
am 15 Mai 2019
function c = regula_falsi(a,b)
Changing yoru function definition like this will return 'c' as your function output. You can also both root and c by placing [root,c] to the left of the equals.
If you want to share specific variables outside of a function, or between fuctions, not just as an output, then you can also define them as global variables.
Geoff Hayes
am 15 Mai 2019
Set your signature to include c as an output parameter
function [root, c] = regula_falsi(a,b)
Please make sure that you assign default values to root and c so that when exiting the function (or "returning" as you do if the two points fail to bracket the root) there is no error.
Akzeptierte Antwort
Sulaymon Eshkabilov
am 15 Mai 2019
Hi Isaac,
An alternative option for your case is to use assignin() within your function file to take out whichever variable/variables you want to save in your workspace and use them later for your analyses.
Good luck.
4 Kommentare
Stephen23
am 17 Mai 2019
Bearbeitet: Stephen23
am 17 Mai 2019
"If you know what you are doing..."
then it is very unlikely that you would use assignin for trivially passing data between workspaces.
"... this is a good alternative to avoid massive outputs, which may get confused when they are many output variables and varargin{} is not well set up. This is the curcial moment why assignin() would be the good option"
It is highly unlikely that assignin is "the good option". In the case of "many output variables" either a structure or nested functions would be much better options (better in the sense simpler, much more efficient, less buggy, easier to debug). The reasons for this have been discussed many times before:
"Don't state what you yourself have not tested in hundreds of cases scenarios!"
Using assignin is almost never a good option (and certainly not for just passing data betweeen workspaces), which should be clear from the MATLAB documentation which recommends to avoid using assignin for trivially passing data between workspaces:
Adam Danz
am 17 Mai 2019
"If you know what you are doing..."
To add to SC's comment, the OP is new to Matlab. It's widely agreed that using dynamic variables is bad practice but since they are so easy to grasp, many beginners use them copiously (my early scripts and functions did, too). In most cases, including this one, alternatives to dynamic variables are much better.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT Files 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!