aboutsummaryrefslogtreecommitdiff
path: root/samples/Matlab
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2012-07-23 15:52:49 -0500
committerJoshua Peek <josh@joshpeek.com>2012-07-23 15:52:49 -0500
commit7b6caa0f6c7603aabb5e6ed657e24aaddbeafc78 (patch)
treeec0522eae75bb04ae73d28ae3f599682a2bfdaeb /samples/Matlab
parent314f0e485227915931db361be14d308a210bedea (diff)
Rename samples subdirectories
Diffstat (limited to 'samples/Matlab')
-rw-r--r--samples/Matlab/average.m9
-rw-r--r--samples/Matlab/make_filter.m38
-rw-r--r--samples/Matlab/matlab_class.m29
-rw-r--r--samples/Matlab/matlab_function.m9
-rw-r--r--samples/Matlab/matlab_script.m12
-rw-r--r--samples/Matlab/matlab_script2.m13
6 files changed, 110 insertions, 0 deletions
diff --git a/samples/Matlab/average.m b/samples/Matlab/average.m
new file mode 100644
index 0000000..65eef8b
--- /dev/null
+++ b/samples/Matlab/average.m
@@ -0,0 +1,9 @@
+function y = average(x)
+% AVERAGE Mean of vector elements.
+% AVERAGE(X), where X is a vector, is the mean of vector
+% elements. Nonvector input results in an error.
+[m,n] = size(x);
+if (~((m == 1) | (n == 1)) | (m == 1 & n == 1))
+ error('Input must be a vector')
+end
+y = sum(x)/length(x);
diff --git a/samples/Matlab/make_filter.m b/samples/Matlab/make_filter.m
new file mode 100644
index 0000000..3ad6c5c
--- /dev/null
+++ b/samples/Matlab/make_filter.m
@@ -0,0 +1,38 @@
+function [filtfcn, statefcn] = makeFilter(b, a)
+% FILTFCN = MAKEFILTER(B, A) creates an IIR filtering
+% function and returns it in the form of a function handle,
+% FILTFCN. Each time you call FILTFCN with a new filter
+% input value, it computes the corresponding new filter
+% output value, updating its internal state vector at the
+% same time.
+%
+% [FILTFCN, STATEFCN] = MAKEFILTER(B, A) also returns a
+% function (in the form of a function handle, STATEFCN)
+% that can return the filter's internal state. The internal
+% state vector is in the form of a transposed direct form
+% II delay line.
+
+% Initialize state vector. To keep this example a bit
+% simpler, assume that a and b have the same length.
+% Also assume that a(1) is 1.
+
+v = zeros(size(a));
+
+filtfcn = @iirFilter;
+statefcn = @getState;
+
+ function yn = iirFilter(xn)
+ % Update the state vector
+ v(1) = v(2) + b(1) * xn;
+ v(2:end-1) = v(3:end) + b(2:end-1) * xn - ...
+ a(2:end-1) * v(1);
+ v(end) = b(end) * xn - a(end) * v(1);
+
+ % Output is the first element of the state vector.
+ yn = v(1);
+ end
+
+ function vOut = getState
+ vOut = v;
+ end
+end
diff --git a/samples/Matlab/matlab_class.m b/samples/Matlab/matlab_class.m
new file mode 100644
index 0000000..7377a4d
--- /dev/null
+++ b/samples/Matlab/matlab_class.m
@@ -0,0 +1,29 @@
+classdef matlab_class
+ properties
+ R;
+ G;
+ B;
+ end
+ methods
+ function obj = matlab_class(r,g,b)
+ obj.R = r;
+ obj.G = g;
+ obj.B = b;
+ end
+ function disp(obj)
+ disp(['Red: ' num2str(obj.R) ...
+ ', Green: ' num2str(obj.G) ...
+ ', Blue: ' num2str(obj.B)]);
+ end
+ end
+ enumeration
+ red (1,0,0)
+ green (0,1,0)
+ blue (0,0,1)
+ cyan (0,1,1)
+ magenta (1,0,1)
+ yellow (1,1,0)
+ black (0,0,0)
+ white (1,1,1)
+ end
+end \ No newline at end of file
diff --git a/samples/Matlab/matlab_function.m b/samples/Matlab/matlab_function.m
new file mode 100644
index 0000000..b73502e
--- /dev/null
+++ b/samples/Matlab/matlab_function.m
@@ -0,0 +1,9 @@
+function ret = matlab_function(A,B)
+% Simple function adding two values and displaying the return value
+
+ret = A+B;
+% Display the return value
+disp('Return value in function');
+disp(ret);
+
+
diff --git a/samples/Matlab/matlab_script.m b/samples/Matlab/matlab_script.m
new file mode 100644
index 0000000..89f1d95
--- /dev/null
+++ b/samples/Matlab/matlab_script.m
@@ -0,0 +1,12 @@
+% Matlab example script
+
+%Call matlab_function function which resides in the same directory
+
+value1 = 5 % semicolon at end of line is not mandatory, only suppresses output to command line.
+value2 = 3
+
+% Calculate sum of value1 and value2
+result = matlab_function(value1,value2);
+
+disp('called from script')
+disp(result);
diff --git a/samples/Matlab/matlab_script2.m b/samples/Matlab/matlab_script2.m
new file mode 100644
index 0000000..8e16eca
--- /dev/null
+++ b/samples/Matlab/matlab_script2.m
@@ -0,0 +1,13 @@
+ % Matlab example script 2
+ % Comments precended with arbitrary whitespace (spaces or tabs)
+
+ %Call matlab_function function which resides in the same directory
+
+value1 = 5 % semicolon at end of line is not mandatory, only suppresses output to command line.
+value2 = 3
+
+ % Calculate sum of value1 and value2
+result = matlab_function(value1,value2);
+
+disp('called from script')
+disp(result); \ No newline at end of file