# HG changeset patch # User Ben Abbott # Date 1313017862 14400 # Node ID 0c74237b34792be1c495ae9ed6b7d3888a72d035 # Parent 9a498efac5f146c62ea44f28a3816b949fd2aca4 str2num.m: Add second output indicating state of the conversion. diff --git a/scripts/strings/str2num.m b/scripts/strings/str2num.m --- a/scripts/strings/str2num.m +++ b/scripts/strings/str2num.m @@ -17,7 +17,8 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {} str2num (@var{s}) +## @deftypefn {Function File} {@var{x} =} str2num (@var{s}) +## @deftypefnx {Function File} {[@var{x}, @var{state}] =} str2num (@var{s}) ## Convert the string (or character array) @var{s} to a number (or an ## array). Examples: ## @@ -33,6 +34,10 @@ ## @end group ## @end example ## +## The optional second output, @var{state}, is logically true when the +## coversion is successful. If the conversion fails the numeric output, +## @var{n}, is empty and @var{state} is false. +## ## @strong{Caution:} As @code{str2num} uses the @code{eval} function ## to do the conversion, @code{str2num} will execute any code contained ## in the string @var{s}. Use @code{str2double} instead if you want to @@ -42,16 +47,18 @@ ## Author: jwe -function m = str2num (s) +function [m, state] = str2num (s) if (nargin == 1 && ischar (s)) [nr, nc] = size (s); sep = ";"; sep = sep (ones (nr, 1), 1); s = sprintf ("m = [%s];", reshape ([s, sep]', 1, nr * (nc + 1))); - eval (s, "m = [];"); + state = true; + eval (s, "m = []; state = false;"); if (ischar (m)) m = []; + state = false; endif else print_usage (); @@ -65,3 +72,8 @@ %!error str2num ("string", 1); +%!test +%! [x, state] = str2num ("pi"); +%! assert (state) +%! [x, state] = str2num (tmpnam); +%! assert (! state)