1

Ok, I am new in Matlab and I am currently working on some econometric script. Before I move to real econometrics I have to create a function that selects the data that I'm interested in. Although I managed to get that script to work by writing at a very structural level, I would like this script to be as universal as possible and therefore would like to divide it into specific functions. However, when I converted all this to one function, I keep getting the error "Function definitions are not permitted in this context". Thanks in advance for your help.

function [probingArray] = extractData (data, startValue, numberOfPeriods)
    arrayHeight=size(data,1);

    for i=1:arrayHeight
        if Date(i)==startValue
            datePosition=i;
        end
    end

    n=1;

    for i=(datePosition-numberOfPeriods):datePosition
        probingArray(n,1)=n;
        probingArray(n,2)=UK(i);
        n=n+1;
    end
clear n i;
Mat
  • 202,337
  • 40
  • 393
  • 406
vatar
  • 11
  • 1
  • 2
  • 2
    Duplicate/Related: [How to correct "Function definitions are not permitted at the prompt or in scripts"](http://stackoverflow.com/questions/5969547/how-to-correct-function-definitions-are-not-permitted-at-the-prompt-or-in-script), [matlab error: Function definitions are not permitted in this context](http://stackoverflow.com/q/5972184/52738), [What's the difference between a script and a function in MATLAB?](http://stackoverflow.com/q/1695365/52738) – gnovice Jul 31 '11 at 19:29
  • Is the code is it's OWN file? BTW, you don't need the clear n, i if it is, since all function variables are local to the function and aren't visible from outside the function. – Marc Aug 01 '11 at 16:01

1 Answers1

0

make sure you respect matlab conventions

function [out1, out2, ...] = myfun(in1, in2, ...) declares the function myfun, and its inputs and outputs. The function declaration must be the first executable line of any MATLAB function.

from http://www.mathworks.com/help/techdoc/ref/function.html

zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • Nope, Date is an array which is imported with a data set before this function is executed – vatar Jul 31 '11 at 19:09
  • there are two more lines on the top (outside the function). Does that matter? – vatar Jul 31 '11 at 19:19
  • a file called extractData.m? just checking the obvious here :) – zeFrenchy Jul 31 '11 at 19:21
  • 1
    if the first two lines are NOT comments, this is your problem – zeFrenchy Jul 31 '11 at 19:25
  • Well, the first one is a comment, while the second one loads the data. I commented it and apparently I don't get the error anymore. I see now that I can't use anything outside the function in one file in Matlab. That's horrible. However, could you please tell me how can I call that function in other script? I presume I have to include the file with that function first.. – vatar Jul 31 '11 at 19:38
  • 1
    Specifically, in the Matlab online doco (which pops up if you run `doc` at the Matlab command line), MATLAB > Getting Started > Programming > Scripts and Functions, and MATLAB > User Guide > Programming Fundamentals > Functions and Scripts should clear this up. – Andrew Janke Aug 01 '11 at 18:50