fabien van mook

all packages "fvm-*"

package "fvm-valuetocode"

function file "valuetocode.m"

Function s = valuetocode(a)
         s = valuetocode(a,p)
         s = valuetocode(a,cfg)
         s = valuetocode(a,p,cfg)

returns a string representation of entity a. Entity a is
a GNU Octave scalar, matrix, cell-array or structure.

There are two precooked representations:
-- GNU Octave code. This is applied at default or when
   input argument p contains 'native', 'octave' or
   'matlab';
-- a code very similar to json. This is applied, when
   input argument p contains 'json-like'.

When input argument p contains 'compact', no spaces, tabs
and newlines in the GNU Octave or json-like code are
printed.

Other representations can be configured by input of a
struct given as input argument cfg, possibly in
combination with input argument p. See into the m-file
valuetocode_cfg.m. This m-file also contains some
information on official json.

Note the following conversions to GNU Octave code:

  empty matrix []            --> empty matrix []
  empty cell-array {}        --> empty cell-array {}
  boolean                    --> numerical value (i.e. 0 or 1)
  character row vector       --> character row vector
  string (entity)            --> (not supported yet)
  1-element matrix           --> scalar
  1-element cell-array       --> 1-element cell-array
  2-dimensional matrix       --> 2-dimensional matrix
  2-dimensional cell-array   --> 2-dimensional cell-array
  >2-dimensional matrix      --> flattened into a 2-d matrix
                                 and wrapped within reshapefrom2d()
  >2-dimensional cell-array  --> flattened into a 2-d cell-array
                                 and wrapped within reshapefrom2d()
  scalar struct              --> rewritten as a 2-d cell-array and
                                 wrapped within structscalar()
  struct array               --> a cell-array for each struct,
                                 which is rewritten as a 2-d
                                 cell-array and wrapped within
                                 structscalar()

The resulting code may contain function calls to the
non-standard functions reshapefrom2d() and/or
structscalar().

Note the following conversions to json-like code:

  empty matrix []            --> empty array []
  empty cell-array {}        --> empty array []
  boolean                    --> numerical value (i.e. 0 or 1)
  character row vector       --> string
  string (entity)            --> (not supported yet)
  1-element matrix           --> scalar
  1-element cell-array       --> 1-element array
  column vector              --> array
  column cell-array          --> array
  row vector                 --> nested array in row-major ordre
  row cell-array             --> nested array in row-major ordre
  >=2-dimensional matrix     --> nested array in row-major ordre
  >=2-dimensional cell-array --> nested array in row-major ordre
  scalar struct              --> object
  struct array               --> nested array in row-major ordre
                                 of objects

A 'nested array' is an array of arrays (of arrays etc.).
E.g. the input of the two-dimensional matrix:
    [1, 2, 3;
     4, 5, 6]
will result in the following nested array in row-major
ordre of json-like code:
    [
      [
        1, 2, 3
      ],
      [
        4, 5, 6
      ]
    ]
So, the two-dimensional matrix or cell-array with m rows
and n columns will be represented as an m-element array of
n-element arrays.

The two precooked conversions illustrate some of the
possibilities of configuration. Note the following
limitations:
-- Some entities at input (notably 1-element matrices,
   booleans and struct arrays) loose their consistency or
   typical values after the conversion.
-- A character matrix at input is internally first converted into
   a column cell-array of character row vectors by cellstr(), before
   further processing.
-- String entities and string arrays are not (yet)
   understood. Only character row vectors have been
   considered in the code of valuetocode(). Here, the term
   'string' is constantly used as synonym of character row
   vector in GNU Octave.
-- Only reals and integers are understood, but no complex
   numbers.
-- Real and integer values are converted basically by
   sprintf(cfg.numfrmt,...), and booleans by
   sprintf(cfg.boolfrmt,...). By default, string
   cfg.numfrmt is '%1.10g' and string cfg.boolfrmt is
   '%1.0f'.
-- The special numeric values NA, NaN and Inf are
   returned as strings by sprintf(), and by default such
   values are also returned by value2json(). It is
   possible to define replacement texts for these special
   values by defining strings cfg.na, cfg.nan and cfg.inf.
-- At default, the output in json-like code may contain
   the special numeric values NA, NaN and Infinity, which
   are not allowed in official json. This is one of the
   reasons, why this representation is called json-_like_.
-- Use the character vectors '_null_', '_true_' and
   '_false_' at input to replace them to json values null,
   true and false, respectively. Other replacements are
   defined by strings cfg.null, cfg.true and cfg.false.
-- Numerical representations (and exponents within the
   numerical representations) may be embellished or
   surrounded by tags by defining strings cfg.numberopen,
   cfg.numberclose, cfg.exponentopen, cfg.exponentclose.
   E.g. 12.34e-5 may be output as
   '<n>12.34<exp>-5</exp></n>'. In such a case, the
   non-standard function tagexponents() is called. At
   default, no embellishments and tags are applied.

Internally, the function valuetocode() may call the
following non-standard m-files:
-- reshapeto1d.m
-- reshapeto2d.m
-- strprefix.m
-- valuetocode_array1d.m
-- valuetocode_array2d.m
-- valuetocode_cfg.m
-- valuetocode_num.m
-- tagexponents.m

At the evaluation of the GNU octave code which has been
output from valuetocode() at default, the following
non-standard m-files may be called:
-- reshapefrom2d.m
-- structscalar.m


(c) 2026 fabien van mook

2026.08.20 release of this file within package "fvm-valuetocode" under GNU GPLv3+