[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
More New Functions: findfigs.m, isfloat.m, genvarname.m
From: |
Bill Denney |
Subject: |
More New Functions: findfigs.m, isfloat.m, genvarname.m |
Date: |
Mon, 10 Mar 2008 21:47:03 -0400 |
User-agent: |
Thunderbird 2.0.0.12 (Windows/20080213) |
Attached are three new functions. Essentially now I'm just going
through the list of functions in matlab that are missing in octave and
writing the low hanging fruit.
Have a good day,
Bill
# HG changeset patch
# User address@hidden
# Date 1205199789 14400
# Node ID a04d848ac6c371575205cbc130ed75035f2e9e4b
# Parent 2e72daff62ba3d194850fff4e519d23a464c45e0
genvarname.m, isfloat.m, findfigs.m: new functions
diff -r 2e72daff62ba -r a04d848ac6c3 scripts/ChangeLog
--- a/scripts/ChangeLog Mon Mar 10 20:19:17 2008 -0400
+++ b/scripts/ChangeLog Mon Mar 10 21:43:09 2008 -0400
@@ -1,3 +1,8 @@ 2008-03-08 Bill Denney <address@hidden
+2008-03-10 Bill Denney <address@hidden>
+
+ * general/genvarname.m, plot/findfigs.m, general/isfloat.m: new
+ functions
+
2008-03-08 Bill Denney <address@hidden>
* geometry/rectint.m: vectorize and add more tests
diff -r 2e72daff62ba -r a04d848ac6c3 scripts/general/genvarname.m
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/genvarname.m Mon Mar 10 21:43:09 2008 -0400
@@ -0,0 +1,94 @@
+## Copyright (C) 2008 Bill Denney
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING. If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} address@hidden =} findfigs (@var{str})
+## @deftypefnx {Function File} address@hidden =} findfigs (@var{str},
@var{exclusions})
+## Create unique variable(s) from @var{str}. If @var{exclusions} is
+## given, then the variable(s) will be unique to each other and to
+## @var{exclusions} (@var{exclusions} may be either a string or a cellstr).
+##
+## If @var{str} is a cellstr, then a unique variable is created for each
+## cell in @var{str}.
+## @seealso{isvarname, exist, tmpnam}
+## @end deftypefn
+
+## Author: Bill Denney <address@hidden>
+
+function varname = genvarname (str, exclusions)
+
+ strinput = ischar (str);
+ ## Process the inputs
+ if (nargin < 2)
+ exclusions = {};
+ elseif ischar (exclusions)
+ if (rows (exclusions) != 1)
+ error ("genvarname: if more than one exclusion is given, it must be a
cellstr")
+ endif
+ exclusions = {exclusions};
+ elseif (! iscellstr (exclusions))
+ error ("genvarname: exclusions must be a string or a cellstr")
+ endif
+ if ischar (str)
+ if (rows (str) != 1)
+ error ("genvarname: if more than one str is given, it must be a cellstr")
+ endif
+ str = {str};
+ elseif (! iscellstr (str))
+ error ("genvarname: str must be a string or a cellstr")
+ endif
+
+ varname = cell (size (str));
+ for i = 1:numel (str)
+ varname(i) = str(i);
+ idx = 0;
+ while any (strcmp (varname{i}, exclusions))
+ idx++;
+ varname{i} = sprintf("%s%d", str{i}, idx);
+ endwhile
+ exclusions(end+1) = varname(i);
+ endfor
+
+ if strinput
+ varname = varname{1};
+ endif
+
+endfunction
+
+## Tests
+## a single argument
+%!assert(genvarname("a"), "a")
+## a single argument with a non-conflicting exception
+%!assert(genvarname("a", "b"), "a")
+## a single argument with a conflicting exception
+%!assert(genvarname("a", "a"), "a1")
+## a single argument as a cell
+%!assert(genvarname({"a"}), {"a"})
+%!assert(genvarname({"a"}, "b"), {"a"})
+%!assert(genvarname({"a"}, {"b"}), {"a"})
+%!assert(genvarname({"a"}, "a"), {"a1"})
+%!assert(genvarname({"a"}, {"a"}), {"a1"})
+## Test different arguments
+## orientation
+%!assert(genvarname({"a" "b"}), {"a" "b"})
+%!assert(genvarname({"a";"b"}), {"a";"b"})
+%!assert(genvarname({"a" "a"}), {"a" "a1"})
+## more than one repetition
+%!assert(genvarname({"a" "a" "a"}), {"a" "a1" "a2"})
+## more than one repetition not in order
+%!assert(genvarname({"a" "b" "a" "b" "a"}), {"a" "b" "a1" "b1" "a2"})
diff -r 2e72daff62ba -r a04d848ac6c3 scripts/general/isfloat.m
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/general/isfloat.m Mon Mar 10 21:43:09 2008 -0400
@@ -0,0 +1,43 @@
+## Copyright (C) 2008 Bill Denney
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING. If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} address@hidden =} isfloat (@var{a})
+## Return true if @var{a} is a floating point data type (single, double)
+## @seealso{isnumeric, isinteger, class, isa}
+## @end deftypefn
+
+## Author: Bill Denney <address@hidden>
+
+function b = isfloat (a)
+
+ b = isa (a, "double") || isa (a, "single");
+
+endfunction
+
+## Tests
+%!assert(isfloat(1), true())
+%!assert(isfloat([1 1]), true())
+%!assert(isfloat([1;1]), true())
+%!assert(isfloat([1 1;1 1]), true())
+%!assert(isfloat(int8(1)), false())
+%!assert(isfloat(uint8(1)), false())
+%!assert(isfloat(true()), false())
+%!assert(isfloat('a'), false())
+%!assert(isfloat("a"), false())
+%!assert(isfloat("ab"), false())
diff -r 2e72daff62ba -r a04d848ac6c3 scripts/plot/findfigs.m
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/plot/findfigs.m Mon Mar 10 21:43:09 2008 -0400
@@ -0,0 +1,63 @@
+## Copyright (C) 2008 Bill Denney
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 3 of the License, or (at
+## your option) any later version.
+##
+## Octave is distributed in the hope that it will be useful, but
+## WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+## General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING. If not, see
+## <http://www.gnu.org/licenses/>.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} findfigs ()
+## Find all figures that are currently visible and off the screen and
+## move them onto the screen.
+## @seealso{figure, get, set}
+## @end deftypefn
+
+## Author: Bill Denney <address@hidden>
+
+function findfigs ()
+
+ figh = get (0, "children");
+ screensize = get (0, "monitorpositions");
+
+ ## give the monitor a margin so that the figure must not just
+ ## marginally be on the monitor.
+ margin = 10;
+ screensize(1:2) += margin;
+ screensize(3:4) -= margin;
+
+ for i = 1:numel (figh)
+ if strcmp (get (figh(i), "visible"), "on")
+ pos = get (figh(i), "position");
+ ## Test if (in order):
+ ## The left side is outside the right side of the screen
+ ## The bottom is above the top of the screen
+ ## The right side is outside the left of the screen
+ ## the top is below the bottom of the screen
+ if ((pos(1) > screensize(3)) |
+ (pos(2) > screensize(4)) |
+ (pos(1)+pos(3) < screensize(1)) |
+ (pos(2)+pos(4) < screensize(2)))
+
+ ## the new position will be at the bottom left of the screen
+ ## (all moved figures will overlap). The bottom left is chosen
+ ## instead of the top left because that allows for the unknown
+ ## amount of space for the menu bar and the title bar.
+ pos(1) = screensize(1);
+ pos(2) = screensize(2);
+ set (figh(i), "position", pos);
+ endif
+ endif
+ endfor
+
+endfunction
- More New Functions: findfigs.m, isfloat.m, genvarname.m,
Bill Denney <=