about functions and arguments

Hi, My question is about the way functions are used and how to use send arguments to them properly. Look at this part of a code for instance:
My function Fitt has just one output and the 'x' dimension is 4*1. In the function i refer to a function that gets artificial neural networks as input. As i try to run the code, the line contain the function doesnt work and it leads to error. I can understand why it could not go through the function. I thought it is perhaps because the Fitt function should include neural networks as argument.Is that true? I must include them? I prefer i could not increase Fitt function arguments ...
Also i had made the networks before and before running this code, i opened them in my workspace. That's why i find the error very weird.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sum =Fitt(x)
inp =QECsimull(net1,net2,t1s,t2s,x);
...
sum=T;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
look forward to hearing from you,
Thanks

 Accepted Answer

First before getting into the problems in the code itself, you have created a new variable SUM that aliases the builtin Matlab function of the same name. While you've done this in the function Fitt() so it will go out of scope when the function exits and not have a global alias of the name, if you were to use the sum() function internal to that program you would get unexpected results. It's good practice to avoid in general regardless of scope.
function mysum =Fitt(x)
inp =QECsimull(net1,net2,t1s,t2s,x);
...
mysum=T;
...
OK, on to your problem -- Matlab variables are in scope in the workspace or in functions unless GLOBAL (or nested functions which aren't present here). So, while you've not given enough code to tell for certain, it's likely the variables net1 net2 t1s t2s are local variables in a calling function or the workspace and so are not visible within the function Fitt().
To fix this you would have to(+) add them to the argument list in the function definition and then pass them in the call.
(+) While it is possible to make them GLOBAL this is poor practice for code maintainability and reliability so is strongly discouraged as a practice in almost all cases.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 25 Jun 2013

Community Treasure Hunt

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

Start Hunting!