[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Writing scripts
From: |
Ciaran Mooney |
Subject: |
Writing scripts |
Date: |
Sat, 8 Dec 2007 21:32:27 +0000 |
Hi,
I have now progressed onto combining what I have learnt from my
previous posts into a script.
The script will plot a graph from a data file, and draw a line of best fit.
I have done this in a two step way, one function generates the graph,
another calls the graph function and calculates a line of best fit.
Unfortunately this didnt work because any values defined in the
"plotgraph" function don't persist.
plotgraph.m
--------------------------------
function [x, y] = plotgraph (file, option)
% This is a function that will take data from a file and plot the values onto
% a graph.
% The file must be in the comma separated value (.csv) format.
% The file can only contain two colums, the first must be x values,
% the second column should be the y values
%
% Options
% * = Plot with *'s
% . = Plot with .'s
% l = Plot with lines
data = load(file);
x=data(:,1); y=data(:,2);
if (option == "*")
plot (x,y, "*")
elseif (option == ".")
plot (x,y,".")
elseif (option == "l")
plot (x,y)
endif
endfunction
--------------------------------------------------------------
function lobf (file)
% This takes data and loads it using the graph function
% It then plots a line of best fit for a linear set of points
% ie. y=mx+c
plotgraph (file, "*");
% Get m and c
p = polyfit(x, y, 1);
% general new y values for x
y2 = polyval(p, x);
% Plot new y values against x
plot (x, y, "*", x, y2)
endfunction
-------------------------------------------------------------
As you can see I have called the "plotgraph" function from within
"lobf". With the intent that x=data(:,1) that was defined from
"plotgraph".
Once I have defined a value in one function how can it be persistant
in octave once the function has ended?
Thanks,
CiarĂ¡n
lobf.m
--------------------------------------------------------------
- Writing scripts,
Ciaran Mooney <=