0

Here is the full error message:

??? Undefined function or variable "Indicator".

Error in ==> vidya at 44
            Indicator(i)=k*sc*Index(i)+(1-k*sc)*Indicator(i+1);

Error in ==> PnL at 22
[Fast] = vidya(Indicator,Shorter_Fast,Longer_Fast);

Error in ==> DEVolSys at 139
            Ftemp=OF(data.Indicator,data.Daily_PnL,Pu(1,ii),Pu(4,ii),Pu(2,ii),Pu(5,ii),Pu(3,ii),Pu(6,ii),MinVol);

Error in ==> Optimser at 15
MA_lengths=DEVolSys(de,dataList,@PnL);

Very occasionally I get the error ???Undefined Function or variable Indicator, when the function below is called.

According to the matlab compiler this line is causing the problem

Indicator(i)=k*sc*Index(i)+(1-k*sc)*Indicator(i+1);

It's odd because usually these problems are linked to functions files being stored in the wrong place or variables being used without being initialised.

However, this function is called repeatedly as part of an optimisation scheme and it seems than most of the time it works perfectly but occasionally I get the error above?

It would seem that a problem parameter set may be causing the problem, but I can't understand how a problem parameter set could cause a problem like this? Also with literally thousands of parameters to test each requiring hundreds of steps I need to go through. I'd like to try and get some idea of what might be causing the problem, so if nothing else I know what to look for.

So if anyone can explain what might be causing this kind of problem or a good way to debug such problems it would be great,

function [Indicator] = vidya(Index,Short,Long)


End_Index=1;

Start_Index=size(Index);

if Short>Long
    TestVal=Short;
else
    TestVal=Long;
end



    for i=Start_Index:-1:End_Index

          if(i>Start_Index-(TestVal+2))    
                      Indicator(i)=Index(i);
        else


        Slow=std(Index(i:i+Long-1));
        Fast=std(Index(i:i+Short-1));

        k=Fast/Slow;
        sc=2/(Short+1);

        Indicator(i)=k*sc*Index(i)+(1-k*sc)*Indicator(i+1);
        end

    end

end

4
  • 2
    Please report the full error message and by the way you have created in mutation which was probably a comment. Commented Aug 13, 2013 at 20:41
  • Several things. Should Start_Index=size(Index); actually be Start_Index=length(Index);, Start_Index=size(Index,1);, or Start_Index=numel(Index);, etc.? And in the line that causes the error you have Indicator(i+1) -is i always one less than the number of elements in that array? Commented Aug 13, 2013 at 20:41
  • try dbstop if error before running the buggy code, that'll throw you in debug mode next time it errors and you can inspect which variable is missing Commented Aug 13, 2013 at 20:44
  • Have you tried calling your function with Index set to a row vector? E.g. vidya(1:10,3,4). This will always result in the error you noted. Commented Aug 13, 2013 at 21:03

1 Answer 1

1

You are just accessing a variable (Indicator) before it has been created.

Matlab doesn't know if Indicator(i+1) is referring to a function or a variable and so generates the ambiguous error message that you are getting.

If during the first executing of your loop code,

if(i>Start_Index-(TestVal+2))  

is false, then

Indicator(i)=k*sc*Index(i)+(1-k*sc)*Indicator(i+1);

will be executed before the variable Indicator has been created.

As Horchler commented, the error is caused by the code

Start_Index=size(Index);

which should be

Start_Index=length(Index);

As I commented, you can reproduce the error by calling vidya with a row vector. For example:

vidya(1:10,3,4)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.