How to run a function in .m octave ?

Discussion in 'Chit Chat' started by blueraincap, Jan 28, 2020.

  1. I am beginner in octave and trying things out.
    I wrote the following and saved it as Ftest.m

    Code:
    pkg load statistics
    function [result, F, leftcutoff, rightcutoff] = Ftest(data, type1error)
      sigma = nanvar(data);
      sigma1 = sigma(1,1);
      sigma2 = sigma(1,2);
      df1 = rows(data(:,1)) - sum(isnan(data(:,1)));
      df2 = rows(data(:,2)) - sum(isnan(data(:,2)));
      if (sigma1 > sigma2)
        F = sigma1/sigma2;
        leftcutoff = finv(type1error/2, df1-1, df2-1);
        rightcutoff = finv(1-type1error/2, df1-1, df2-1);
      else
        F = sigma2/sigma1;
        leftcutoff = finv(type1error/2, df2-1, df1-1);
        rightcutoff = finv(1-type1error/2, df2-1,df1-1);
      endif
      if (F>leftcutoff && F<rightcutoff)
        result = "do not reject";
      else
        result = "reject";
      endif
    endfunction
    
    
    I then use addpath('C:\Users\Hello\Dropbox\Octave\') so Octave can find the file.
    I then try calling it by Ftest(sampleData, 0.05), but getting the following msg:
    warning: function 'Ftest' defined within script file 'C:\Users\Hello\Dropbox\Octave\Ftest.m'
    error: invalid call to script C:\Users\Hello\Dropbox\Octave\Ftest.m
     
    Last edited: Jan 28, 2020
  2. It is working after I clicked 'run' which I guess compiles the .m file. I didn't realise that
     
    Last edited: Jan 28, 2020
  3. Actually, I realised that I couldn't call the function (Ftest(args)) because of the file being a script file instead of a function file. Function file can only contain a single-function definition, and that pkg load statistics at the beginning made it not one.

    If a function file should only contain one single function def with file name = function name, is it not possible to define multiple functions inside one file similar to defining many static methods in a utility class in java? I wanna have a file for say 1-sample data, 2-sample data, k-sample data, etc and define multiple functions for each data type, how can that be done?