help-octave
[Top][All Lists]
Advanced

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

Re: Indexing array elements inside a cell in C++ API


From: Pantxo
Subject: Re: Indexing array elements inside a cell in C++ API
Date: Mon, 27 Jan 2020 10:39:44 -0600 (CST)

rupakrokade wrote
> In octave if you do c = {[1;2]}, it will result in the following output.
> *c =
> {
>   [1,1] =
> 
>      1
>      2
> 
> }*
> 
> The problem is I do not know a way to access the second element of the
> array. Doing c(2,1) says
> 
> *error: c(2,_): but c has size 1x1*
> 
> Notice that it says the size is 1x1 whereas the array is clearly of size
> 2x1.
> 
> I actually want to index the array inside the cells within a C++ code
> using
> Octave's API. *feval()* returns me a cell containing such an array and I
> am
> struggling to index it. Please help.
> 
> 
> 
> --
> Sent from:
> https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

The top container, the cell array, actually only contains 1 element, the
double vector. In  Octave you would have to do

vec = c{1};
vec(2,1);

In C++, you'll want to do something equivalent. Suppose args is the input
octave_value_list and contains your cell variable:


#include <octave/oct.h>

DEFUN_DLD(toto, args, nargout,
          "-*- texinfo -*-\n\
@deftypefn {} {@var{retval} =} toto (@var{input1})\n\
@seealso{}\n\
@end deftypefn")
{
  octave_value_list retval;

  if (args.length () < 1)
    error ("toto: expects one argument");
  
  Cell c = args(0).xcell_value ("First argument should be a cell");

  if (c.numel () < 1)
    error ("toto: expect the cell to contain a matrix");
  
  Matrix m = c(0).xmatrix_value ("First element of the cell should be a
matrix");
  octave_stdout << "First element of the matrix : "<< m(0,0) << std::endl;
  
  return retval;
}

HTH,

Pantxo



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

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