First you are generating the numbers from the wrong starting point, you are told to use f(1)=1, f(2)=2, but no matter it works better starting with f(1)=1, f(2)=1.

You don't need to store the numbers, and you have to sum every third one, the following may work (its untested):

CBCode:a=1;b=1; sum=0; %running total of the even Fib. numbers for idx=3:4000000 %loop over a sufficient range no get a Fib. number over 4000000 n=a+b; %next Fib number a=b;b=n; %reset last two numbers for next trip arround loop if n>4000000 %exit loop if current number >4000000 break end if mod(idx,3)==0 %if this is an even Fib number add so running total sum=sum+n; end end