Problems with making variables global
Afficher commentaires plus anciens
I'm trying to make two separate functions,
- function 1,DrawRecCircle(vs,vr,a,b,alpha,beta,delta), draws some vectors
- function 2,DrawCircle(xRecCent,yRecCent,(vs*vr/b)); draws a circle partly using the values of the first function
Now the problem is, the first two arguments of function 2, are local variables of function 1. I have used "global" in function 1 to do the job, to make them available to use by function 2, but when I run the program, those variables (xRecCent and yRecCent) just don't get global, and function 2 can't use them, in fact when i check their values, they appear to have no value.
Here's the part of the code of the M file that matters
DrawRecCircle(vs,vr,a,b,alpha,beta,delta) % all of these vars are defined but not mentioned here
DrawCircle(xRecCent,yRecCent,(vs*vr/b));
function 1 Code:-
function DrawRecCircle(vs,vr,a,b,alpha,beta,delta)
%The center of the circle
xRecCent= -( (((vr^2)*a)/b) * (cosd(beta-alpha))); %X-Coordinate of Center in MW
yRecCent= -( (((vr^2)*a)/b) * (sind(beta-alpha))); %Y-Coordinate of Center in MW
%The radius of the circle
xRecRad= ( ((vs*vr)/b) * (cosd(beta-delta)) ) - ( (((vr^2)*a)/b) * (cosd(beta-alpha))); %Load Line End X-Coordinate of Radius in MW
yRecRad= ((vs*vr)/b) * (sind(beta-delta)) - ( (((vr^2)*a)/b) * (sind(beta-alpha))); %Load Line End Y-Coordinate of Radius in MW
%The load line
xl = xRecRad;
yl = yRecRad;
%Plotting the Circle Diagram Vectors
plot([0,xRecCent],[0,yRecCent],[xRecCent,xRecRad],[yRecCent,yRecRad],[0,xl],[0,yl],[-20000 20000],[0 0], 'k-',[0 0],[-20000 20000]);
global xRecCent yRecCent xRecRad yRecRad
end
function 2 Code:-
function DrawCircle(x,y,rad) %Draws the Circle Shape
ang=0:0.01:2*pi; %Defining domain
xp=rad*cos(ang); %Defining the radius of one semi-circle
yp=rad*sin(ang); %Defining the radius of the other semi-circle
plot((x+xp),(y+yp),'r --'); %Plotting the circle shape, with a red dotted pattern
end
- I have tried to place the global command everywhere in the .m file, and in the function files but i still get the same problem, xRecCent and yRecCent have the value []. I am running R2014B
- I got function 2 from this post, i did not write it myself.https://www.mathworks.com/matlabcentral/answers/3058-plotting-circles
Réponses (1)
Walter Roberson
le 13 Oct 2016
0 votes
The "global" must be used before the first reference to the variable. It must also be used in every routine that assigns to or reads from the variable that is to be global.
There are typically better ways, such as shared variables.
Catégories
En savoir plus sur Shifting and Sorting Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!