[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What's the correct order of things in a file? (beginner's question)
From: |
Robert A. Macy |
Subject: |
Re: What's the correct order of things in a file? (beginner's question) |
Date: |
Mon, 13 Jun 2005 20:42:00 -0700 |
Here is a sample function program called
ppm.m
It shows decent practice for "help" section and error
checking inputs that go into the function.
WATCH OUT FOR WORD WRAPPING ! ! !
=-=-=-=-=-=start of file=-=-=-=-=
function [output,outputrms]=ppm(input,start,stop);
% PPM returns the parts per million error from the mean of
the matrix
% use form [output, outputrms]=ppm(input,[start, stop]);
% where input is a matrix
% returns the ppm variation and a vector showing rms of
that variation
% optional start and stop reference where the average is
taken
% default is 1 and rowsinput, or end
if (nargin==0)
help ppm;
return;
endif
if (nargin>3)
error("only one, or three, arguments \n");
return;
endif
if (nargin==2)
error("if define start, must define stop \n");
return;
endif
[rowsinput,columnsinput]=size(input);
if (rowsinput==1)
error("input must have at least two rows \n");
return;
endif
if (nargin==1)
start=1;stop=rowsinput;
endif
if ( (isscalar(start) !=1) + (isscalar(stop) != 1) )
error("start and stop must be single real integer values
/n");
return;
endif
if ( (abs(mod(start,1)) != 0) + (abs(mod(stop,1)) != 0) )
warningmsg="start and stop should be integers, rounding
off... "
start=round(start);stop=round(stop);
endif
if (start >= stop)
error("start cannot be equal to, or larger than, stop
\n")'
return;
endif
if (stop > rowsinput)
error("stop is outside range of input \n")'
return;
endif
inputave=ones(rowsinput,1)*mean(input(start:stop,:));
output=2000000*(input-inputave)./(abs(input)+abs(inputave)+eps/1e12);
test=abs(output)-ones(rowsinput,1)*mean(abs(output));
outputrms=sqrt(mean(test.*test));
=-=-=-=-=-=end of file=-=-=-=-=-=
On Mon, 13 Jun 2005 18:48:13 -0700
David Collett <address@hidden> wrote:
> Hi, Robert.
>
> Thanks for helping me.
>
> This makes a lot more sense now. I can see the advantages
> of saving a function as a separate file to reuse with
> other scripts.
>
> I'll try it the way you suggest tonight.
>
> Thanks again.
>
> David
>
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------