[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Constants in functions?
From: |
Windhorn, Allen E [ACIM/LSA/MKT] |
Subject: |
Constants in functions? |
Date: |
Thu, 5 Mar 2020 18:12:11 +0000 |
What's the accepted way to define a constant inside a function? Example:
____________________________________
function [sc, err] = symmcomp(abc)
% Function accepts [abc] complex components and delivers symmetrical
% components as a vector [x0, x1, x2] of complex values
persistent a = complex(-0.5, sqrt(3)/2); % Rotation vector
%
% Build conversion matrix_type (constant)
persistent A = [1, 1, 1;
1, a, a^2;
1, a^2, a]./3;
%
% Make abc a column vector
if (length(abc)>3)
sc = [];
err = "Too many components!";
elseif (length(abc)<3)
sc = [];
err = "Too few components!";
else
abc = reshape(abc,[3,1]);
sc = A*abc;
err = "";
endif
endfunction
________________________________________
Here a is a constant and A is a constant matrix. I used persistent in hopes
it would prevent them from being redefined every time the function is called.
In C++ I would use the "const" keyword.
Regards,
Allen
--
Allen Windhorn P.E. (Mn), CEng| Senior Principal Engineer
Leroy-Somer Americas | Kato Engineering, Inc.
2075 Howard Dr. West | North Mankato, MN 56003 | USA
T +1 507-345-2782 | F +1 507-345-2798
address@hidden | address@hidden
- Constants in functions?,
Windhorn, Allen E [ACIM/LSA/MKT] <=