View Raw SPL
/*****************************************************************************
*                                                                            *
*   CELL.SPL     Copyright (C) 2021 DSP Development Corporation              *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns an empty cell array                                 *
*                                                                            *
*   Revisions:    1 Oct 2021  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/

#include 

#if @HELP_CELL

    CELL

    Purpose: Creates an empty cell array

    Syntax:  CELL(numrows, numcols, numpages)

              numrows  - An integer, the number of rows.

              numcols  - Optional. An integer, the number of columns.
                         Defaults to NUMROWS.

              numpages - Optional. An integer, the number of pages.
                         Defaults to 1.

    Returns: An empty cell series or array

    Example:
             cell(3)

             returns a 3x3 empty cell series.

    Example:
             cell(3, 1)

             returns a 3x1 empty cell series.

    Example:
             cell(3, 2)

             returns an empty 3x2 cell array.

    Example:
             cell(3, 2, 5)

             returns an empty 3x2x5 cell array.

    Remarks:
             cell(size(A)) returns an empty cell array with same dimension
             as A.

    See Also:
             Ones
             Size
             Zeros

#endif


/* generate an empty cell series or array */
cell(a, b = a, c = 0)
{
        local nr, nc, out, j;

        if (argc > 1)
        {
                /* e.g. cell(10, 3) */
                if (isscalar(a) && isscalar(b))
                {
                        /* convert to ints */
                        a = castint(a);
                        b = castint(b);
                        
                        if (a <= 0 || b <= 0)
                        {
                                /* empty cell */
                                out = [];
                        }
                        else
                        {
                                out = defineobject(a, b, 15);

                                if (c > 0)
                                {
                                        /* 3D cell */
                                        loop (j = 1..(a * b))
                                        {
                                                out[j] = defineobject(c, 1, 15);
                                        }
                                }
                        }
                        
                        return(out);
                }
                else
                {
                        error(sprintf("%s - series or number required", __FUNC__));
                }
        }
        else
        {
                if (argc < 1) return([]);

                if (isarray(a))
                {
                        /* series argument - get size */
                        (nr, nc) = size(a);
                        
                        if (nr == 2 && nc == 1)
                        {
                                /*
                                 *  assume series came from size function,
                                 *  e.g. cell(size(a))
                                 */
                                
                                b = int(a[2]);
                                a = int(a[1]);
                        }
                        else
                        {
                                /* e.g. cell(x) */
                                a = nr;
                                b = nc;
                        }
                }
                else if (isscalar(a))
                {
                        /* convert to ints */
                        a = castint(a);
                        b = a;
                }
                else
                {
                        error(sprintf("%s - series or number required", __FUNC__));
                }
                
                if (a*b == 0)
                {
                        /* empty cell */
                        out = [];
                }
                else
                {
                        out = defineobject(a, b, 15);
                }
                
                return(out);
        }
}