Borrar filtros
Borrar filtros

how to make a global function ?

70 visualizaciones (últimos 30 días)
tomer polsky
tomer polsky el 9 de En. de 2018
Editada: Stephen23 el 9 de En. de 2018
In my program there is is a function that stores all the values of my circuit,how ever i want to make those values golobal (meanining that i can use this varblues in other scripts ).
1) my question is it possible to make a function global ?
2)and from what i undestand using global command is bad programing could you explain why is that ?
3)and what use insted of global command?
thank you
  3 comentarios
tomer polsky
tomer polsky el 9 de En. de 2018
hello i treid what you said but it did not work ,for exmaple i have this function :
function [ P_out_sepic, V_desire,R,U,C,C1,L1,L2,R_C,R_C1,R1,R2,R_diode,V_diode ] = values_of_circuit( P_out_sepic, V_desire,R,U,C,C1,L1,L2 )
P_out_sepic=100;
V_desire=18;
R=(V_desire*V_desire)/ P_out_sepic;
U=12;
C=1000e-6;
C1=1000e-6;
L1=330e-6;
L2=330e-6;
R_C=0.05;
R_C1=0.05;
R1=0.1;
R2=0.1;
R_diode=0.0000000001;
V_diode=0.3;
end
now i want to use the values of this function in other function for exmaple for this fucntion :
function [finding_Duty_cycle]=find_DC(alpha);
sym alpha
t
V=((R + R_C)*(- R*V_diode*alpha^2 + R*U*alpha))/(R*R1 + R1*R_C + R^2*alpha^2 + R*R_C*alpha + R*R_C1*alpha + R_C*R_C1*alpha + R*R2*alpha^2 + R*R_C*alpha^2 + R2*R_C*alpha^2);
eqn1=V== V_desire;
Solve_alpha=double(solve(eqn1,alpha));
finding_Duty_cycle=(100)./(1+ Solve_alpha)
end
how do i pass the varibles of the first function with out using global ?

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de En. de 2018
The first function in any function .m file (not script, not classdef) is "global" in the sense of being accessible to any other function. There are exceptions for private methods (in directories named private) and for class directories (you cannot add a @ directory to the path), but otherwise they are already global.
However, the ability to access a function anywhere does not mean that any variable created in the function is available everywhere. Values created in functions only exist while the function is running, with the exception of variables declared persistent (and with exceptions for some cases involving nested functions, or anonymous functions.)
It is a valid programming method to have a function that looks something like,
function r = get_param(param_name)
persistent alpha beta radiation
if isempty(alpha)
alpha = ...
beta = ...
radiation = ...
end
if ~exist('param_name','var') || isempty(param_name) || ~ischar(param_name)
r = [];
else
switch param_name
case 'alpha'
r = alpha;
case 'beta'
r = beta;
case 'radiation';
r = radiation;
otherwise
r = [];
end
end
end

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by