How can we call a variable defined in a function file to another script file?

49 views (last 30 days)
I want to call a variable that is defined in a function file to another script file. In a naive way, I assigned this variable as an output of the function, however, this did not help me. That is, MATLAB still returns an error that the variable is not defined. Then, I realized that this may be because the output variable I am defining does not depend on the input variables of the function. (I do not know whether my realization is correct or not.)
Thus, I want to call a variable in a script file that is defined in another function file. Please help me how can I do this?
Thank you.
  14 Comments
erick oibi
erick oibi on 10 May 2018
@stephen Hey , I have a question, how do you initialize a variable in a script to a value obtained from a function. I have obtained power loss in the loadflow function. I want to initialize the base power loss in a script to the value obtained from the power loss, how should I go about.Kindly help.
dpb
dpb on 11 May 2018
Since a script is global scope, simply using the variable name as the LHS in the statement that calls the function will do so in the script which would be the "clean" way; then everything is within the script itself.

Sign in to comment.

Answers (1)

dpb
dpb on 2 Dec 2017
Well, you had the right idea; you didn't show us the syntax used but you must refer to the variable that is returned from the function to use it elsewhere; variables are local within functions so what they're called there is totally immaterial to what you use externally.
function returnVar=YourFunction(inputVar)
% do something here
...
returnVar=theResultofThatSomething; % set the returned variable in the function
Your script wanting to compute and use whatever it is YourFunction does looks like--
% whatever else goes on before in the script here
...
x=32.4; % some arbitrary initial value
y=YourFunction(x); % compute the result with that input
% Now y has the result from the function in the script
...
% Do whatever is needed/wanted with y here...
...
  6 Comments
vikas mishra
vikas mishra on 3 Dec 2017
@ dbp thank you for your continuous support. You are right. It did not work. Finally I could do it by defining theta_0 as global variable. Your ideas helped me carrying out this. So thank you very much once again.
dpb
dpb on 3 Dec 2017
Edited: dpb on 3 Dec 2017
BAD IDEA!!! DO NOT use GLOBAL here; it's bad design and will lead to continuing such with time.
Fix the logic as outlined previously--there's no reason if the theta_0 range is to be hardcoded for it to be buried in the function; that makes the function of little generic value; instead write the function generically and then you can call it with a desired range from the caller and the problem of finding out the values within the function goes away entirely.
function p=whiskerShapeLoop_res(IC_guess, theta_0)
xcf=14.87;
ycf=4.71;
r=0.25;
for theta=deg2rad(theta_0)
[x,y] = ode45(@whiskerShape_model, [0 1],[0 0 theta IC_guess(1) IC_guess(2) IC_guess(3) 0]);
p = [y(end,1)-(xcf+r*sin(y(end,5))), y(end,2)-(ycf-r*cos(y(end,5))), y(end,3)-y(end,5)];
...
and in the script use
theta_0=0:10:21;
or even better would be something like--
dtheta=10; % set incremental theta
thethaUp=21; % upper limit for theta
theta_0=[0:dtheta:thetaUp]; % set the range
p=whiskerShapeLoop_res(IC_guess,theta); % call the routine
...
This way you have a much more flexible toolset that can be utilized for any range of inputs that comes up; not just the one specific one simply by modifying the constants. Or, of course, generalize even further by reading the constants or getting them from the user on the fly so don't have to make any code changes to run different cases.
NB: Of course, this still doesn't solve the problem that the loop inside the function is pointless--you don't return anything except the result after the loop is completed; all the intermediary results are calculated by "thrown away" by the reassignment of each result into the same variable, p.
It's not clear what you're really after for certain, but if it is the result for each element of the vector of starting conditions, then you'll need to save that as an array in the function.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!