MATLAB: Calling functions from different scripts

errorfunctionsscript

%func.m function f=transs(alpha) f=exp(-alpha)+alpha-1 end
i wrote this function in a script,but when i try calling it in another script using transs(theta),it keeps giving me an error saying alpha is not defined, i called func.m by writing func; in the code,why am i getting this error. func.m works through the command window though.The following is the second script i wrote calling func.m.
init1; func; theta=1:0.5:20 H11=zeros(1,numel(theta)); H12=zeros(1,numel(theta)); H21=zeros(1,numel(theta)); H22=zeros(1,numel(theta));
data= zeros(1,numel(theta)); for i=1:numel(theta) H11(i)=(theta(i)^2)/2-transs(theta(i)); H12(i)=(theta(i)^2)/2-transs(theta(i)); H21(i)=(theta(i)^2)/(2*epsilon1)-(epsilon1*transs(theta(i)/epsilon1)); H22(i)=(theta(i)^2)/(2*epsilon2)-(epsilon2*transs(theta(i)/epsilon2)); end

Best Answer

Firstly, format your code so it's legible...use two blanks in front of first code line in a new paragraph. Keep at it until it looks right in the code window....
The reason alpha is undefined in the second invocation is that you didn't pass any arguments when you called func as shown below...
nit1;
func; % where's the required argument???
...
Also, what's the point in calling the function if you don't provide any return variable? There's no effect in the calling routine of having done so without it.
Also, it's very bad form (and bound to be confusing at best) to not name the m-file the same as the function name. What are you going to do when you want a second function and you've already used func as a name? Name the m-file in the case of your function transs.m and then call it as
fval=transs(inputalpha);