How to use a variable only defined in a function in your script?

21 visualizzazioni (ultimi 30 giorni)
Hi,
I created a function that takes the user's values for the angle and I want to use those values in my script but when I try putting my function in the script and running it, Matlab tells me that variable is undefined.
Would making the variable global fix this or is this only between functions? (If it works please show me how to make a variable global).
THANKS!
  5 Commenti
Samy Ben Thabet
Samy Ben Thabet il 25 Ott 2018
Alright thank you. Do you know what's wrong with my code?
madhan ravi
madhan ravi il 25 Ott 2018
don't use angle as your function name it may contradict which inbuilt function name which returns phase angle!

Accedi per commentare.

Risposte (3)

Image Analyst
Image Analyst il 25 Ott 2018
You need to return the value in your function to see in in the calling script. Like this
[var1, var2] = ComputeValue(input1, input2); % Call the function
% Define the function
function [var1, var2] = ComputeValue(input1, input2); % Call the function
% some code..... then
var1 = .... whatever.
var2 = .... whatever.
If it doesn't say the variable names on the function line, and you don't accept those values into the main calling script, then you won't have those values in the main calling script.
  2 Commenti
Samy Ben Thabet
Samy Ben Thabet il 25 Ott 2018
I am not sure that I understand what you mean.
Here is a part of my code:
y0=input('Enter the initial vertical position:');
v0=input('Enter the initial velocity:');
angle(theta_min,theta_max,theta)
for i=1:length(theta)
ProjectileMotion(x0,y0,v0,theta(i));
end
and here is the "angle" function:
function t=angle(theta_min,theta_max,theta)
theta_min=deg2rad(input('Enter the lower value of the angle range (deg):'));
theta_max=deg2rad(input('Enter the upper value of the angle range (deg):'));
theta=linspace(theta_min,theta_max,10);
end
Image Analyst
Image Analyst il 25 Ott 2018
MATLAB is not call by reference - it's call by value. So you have to return values for them to change in the calling routine, and you don't pass them in as input arguments. And you don't pass back t from angle() since t is never even defined. Do it like this:
function [theta_min, theta_max, theta] = angle()
theta_min=deg2rad(input('Enter the lower value of the angle range (deg):'));
theta_max=deg2rad(input('Enter the upper value of the angle range (deg):'));
theta = linspace(theta_min, theta_max, 10);
end
Then, when you call it, instead of doing this:
angle(theta_min,theta_max,theta) % Wrong way!
you do this:
[theta_min, theta_max, theta] = angle() % Right way.

Accedi per commentare.


Stephen23
Stephen23 il 25 Ott 2018
Your usage of the function angle has several bugs, in particular:
  1. You do not call angle with any output arguments, although you need it later in your code.
  2. You define three input arguments to angle, but ignore all of them inside the function.
  3. You define one output argument to angle, but do not define it anywhere inside the function itself.
Here is simpler code without a function:
x0 = str2double(input('Enter the initial horizontal position: ','s'));
y0 = str2double(input('Enter the initial vertical position: ','s'));
v0 = str2double(input('Enter the initial velocity: ','s'));
theta_min = str2double(input('Enter the lower value of the angle range (deg): ','s'));
theta_max = str2double(input('Enter the upper value of the angle range (deg): ','s'));
theta = linspace(deg2rad(theta_min),deg2rad(theta_max),10);
for k = 1:numel(theta)
ProjectileMotion(x0,y0,v0,theta(k));
end
If you really want to use a function named angle, then use this:
function t = angle(t_min,t_max)
t = linspace(t_min,t_max,10);
end
and then call it like this:
theta = angle(deg2rad(theta_min),deg2rad(theta_max));
But to be honest, I don't see much point in doing that.
Note that ProjectileMotion also has three output arguments, which you ignore totally when you call the function.
  2 Commenti
Samy Ben Thabet
Samy Ben Thabet il 25 Ott 2018
I know that I don't need to create a function for the angle but I use it multiple times through out my code and I felt like a function would make it look better. I also have other variables that I use multiple times therefore I wanted to create a function for each one.
As for ProjectileMotion I did not ignore the output arguments, I did not upload it because it works. I only had a problem with other functions.
I'm not sure I understand the function you gave me, it doesn't ask the user for the inputs.
Stephen23
Stephen23 il 25 Ott 2018
Modificato: Stephen23 il 25 Ott 2018
"I'm not sure I understand the function you gave me, it doesn't ask the user for the inputs."
Because I moved those input calls to outside of the function. Your function was ambiguously written because you had both input arguments (which got discarded) and input calls (which redefined the input arguments). It is better to use the input arguments, which is why I moved the input calls outside the function. You can easily move them back inside, if you wish:
function t = angle() % no input arguments!
t_min = str2double(input('Enter the lower value of the angle range (deg): ','s'));
t_max = str2double(input('Enter the upper value of the angle range (deg): ','s'));
t = linspace(deg2rad(t_min),deg2rad(t_max),10);
end
And then call it like this:
x0 = str2double(input('Enter the initial horizontal position: ','s'));
y0 = str2double(input('Enter the initial vertical position: ','s'));
v0 = str2double(input('Enter the initial velocity: ','s'));
theta = angle(); % no input arguments!
for k = 1:numel(theta)
ProjectileMotion(x0,y0,v0,theta(k));
end

Accedi per commentare.


madhan ravi
madhan ravi il 25 Ott 2018
x0=0
theta_min=deg2rad(10);
theta_max=deg2rad(60);
y0=2;
v0=3;
theta=angle1(theta_min,theta_max)
for i=1:length(theta)
ProjectileMotion1(x0,y0,v0,theta(i));
end
function theta=angle1(theta_min,theta_max)
theta=linspace(theta_min,theta_max,10);
end
function [h_max,range,max_d]=ProjectileMotion1(x0,y0,v0,theta)
g=9.81;
a=1/2*g/(v0*cos(theta))^2;
b=-tan(theta);
c=tan(theta)*x0-y0-g/2/(v0*cos(theta))^2*x0^2;
x_max=(-b+sqrt(b^2-4*a*c))/2/a;
t_max=(x_max-x0)/(v0*cos(theta));
t=linspace(0,t_max,1000);
x=x0+v0*cos(theta).*t;
y=y0+v0*sin(theta).*t-1/2*g*t.^2;
h_max=max(y);
max_d=max(x);
range=max_d-x0;
hold on
plot(x,y)
hold off
fprintf('The possible maximum height is %.3f meters\n',h_max)
fprintf('The possible range is %.3f meters\n',range)
end

Community Treasure Hunt

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

Start Hunting!

Translated by