help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Cells?


From: Paul Kienzle
Subject: Re: Cells?
Date: Fri, 1 Feb 2002 14:31:52 -0500

You are right :-(   You can create cell arrays but not dereference them.

There is a patch available in the octave-sources list to improve cell array
support, but AFAIK this has not been applied to the main Octave
distribution.  In my version of the patch, you have to use the "nth"
function to dereference a cell.  I don't remember how the latest patch does
it.  It may use automatic type promotion so that x(i,j) returns the
contents of the cell i,j rather than returning a 1x1 cell array which
contains x(i,j) as matlab does.  Since {} and () are equivalent in octave,
this will mostly do the right thing for pre-existing matlab code.

Either you can get the patch and apply it yourself, or you can compile
the following function deref.cc and replace all instances of
x{i,j} on the right-hand-side by deref(x,i,j).  For completeness, I
suppose you also need assign(x,i,j,v) to replace all instances of
x{i,j} = v, but that I leave as an exercise for the reader.

Paul Kienzle
address@hidden

#include <octave/oct.h>
#include <octave/ov-cell.h>

DEFUN_DLD (deref, args,,
"-*- texinfo -*-\n\
@deftypefn {Built-in Function} {} deref (@var{cell}, @var{n})\n\
Return the @var{n}-th element of @var{cell}.\n\
@deftypefnx {Built-in Function} {} deref (@var{cell}, @var{i}, @var{j})\n\
Return the @var{i,j}-th element of @var{cell}.\n\
@end deftypefn")
{
  octave_value retval;
  int i=-1,j=-1;
  
  if (args.length() < 2 
      || (args(0).is_list() && args.length() > 2)
      || args.length() > 3)
    {
      print_usage ("deref");
      return retval;
    }
  
  i = args(1).int_value (true);
  if (error_state || i < 1)
    {
      error ("deref: second argument must be a positive integer");
      return retval;
    }
  
  if (args.length() == 3) 
    {
      j = args(2).int_value (true);
      if (error_state || j < 1)
        {
          error ("deref: third argument must be a positive integer");
          return retval;
        }
    }
  
  Cell cell = args(0).cell_value ();
      
  if (! error_state)
    {
      int nr = cell.rows();
      int nc = cell.columns();
      
      if (j == -1) 
        if (nr == 1)
          if (i <= nc)
            retval = cell(0,i-1);
          else
            error ("deref: index = %d out of range", i);
        else if (nc == 1)
          if (i <= nr)
            retval = cell(i-1,0);
          else
            error ("deref: index = %d out of range", i);
        else
          error ("deref: single index not valid for 2-D cell array");
      else if (i <= nr && j <= nc)
        retval = cell(i-1, j-1);
      else
        error ("deref: index = %d,%d out of range", i, j);
    }

  return retval;
}

On Fri, Feb 01, 2002 at 10:28:49AM -0600, Michael W. Martin wrote:
> Greetings,
> 
> Our in-house simulation written in C reads certain data files that have 
> entries such as this:
> 
>       Some_variable = { 1.2, 2.3, 3.4 };
> 
> This, of course, is the same format as matlab's cells and it is fairly 
> easy to get matlab to read these files.  Octave seems to be another matter.
> 
> My present version of octave is 2.1.35. It ACCEPTS this data form well 
> enough, and it will even correctly print the data form. However, it will 
> not allow any extraction of the data into anything useful, at least not 
> one that I could find. Not matter how I assign the elements of the cell, 
> it produces a cell which cannot be manipulated computationally. I get the 
> impression that creation of cells is supported, but not their manipulation.
> Suggestions? Hints?
> 
> ----------------------------------------------------------------------
> Michael W. Martin                   Phone: (281) 333-2177
> Draper Laboratory                   FAX:   (281) 333-5276
> 2200 Space Park Dr.                 EMail: address@hidden
> Houston, TX 77058                   WWW: http://www.jsc.draper.com/
> USA                                 Mail Code: EG/Draper
> ----------------------------------------------------------------------
> 
> 
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------
> 



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]