help-octave
[Top][All Lists]
Advanced

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

Re: Need help with structures and "eval" function


From: A Scottedward Hodel
Subject: Re: Need help with structures and "eval" function
Date: Mon, 20 Mar 2000 08:24:43 -0600

Date: Mon, 20 Mar 2000 08:23:57 -0600
To: Gerrit Visser <address@hidden>
From: A Scottedward Hodel <address@hidden>
Subject: Re: Need help with structures and "eval" function
Cc:
Bcc:
X-Attachments:

Hi All,

I hope I'm sending this to the correct octave help mailing list.

I'm still using 2.0.13 (I think), and if upgrading would help solve my
problems I'll gladly do it. On to my problems:

Is there some way I can either execute the "eval" command from a c++
(.oct) environment or to ask the octave session to evaluate it and return
a result? I've seen something like this in the Matlab .mex method, but
couldn't find anything usefull in the octave help. The source files for
octave isn't much help either, as I do not have a month to trace all the
classes / functions (probably a optimistic estimate of the time!)


I think you're looking for the feval function; look in the help-octave
archives at http://www.che.wisc.edu/octave.


The second problem I have is with passing structures to C++. Is there
somewhere in the help files I can find out how to handle them? The
reason I need it is the following: For a certain optimisation problem, the
cost function is a generic one. The cost function needs to call a function
specified in one of it's argument lists, so that it can call the correct
function for evaluation:


I haven't used this .oct feature in awhile, so the syntax may be out of
date, but here's a snippet of some code I used with the Octave controls
toolbox a few years ago:

Structures are created and manipulated as "Octave_map" types (such as the
variable mp below):
//
// p a c k : TF form
// pack transfer function coefficients into an octave structure
//

Octave_map pack(const RowVector& num, const RowVector& den,
        const string_vector& inname, const string_vector& outname, double tsam)
{
  #ifdef DEBUG
  cout << "pack(TF form)" << endl;
  #endif
  Octave_map mp;

  // construct sys vector
  RowVector sys(4);
  for( int ii = 0; ii < 4 ; sys(ii++) = 0);
  sys(1) = 1;

  mp["sys"] = octave_value(sys,0);   // make it a row vector
  mp["inname"] = inname;
  mp["outname"] = outname;
  mp["num"] = octave_value(num,0);
  mp["den"] = octave_value(den,0);
  mp["tsam"] = tsam;
  int n, nz;
  if( tsam > 0)
  {
    mp["yd"] = (double) 1;
    n = 0;
    nz = den.length() - 1;
  }
  else
  {
    nz = 0;
    n = den.length() - 1;
    mp["yd"] = (double) 0;
  }
  mp["n"] = (double) n;
  mp["nz"] = (double) nz;
  string_vector stname = sysdefstname(n,nz);
  mp["stname"] = stname;

  return mp;
}


=======================================
Values may be extracted from Octave_map variables as shown below:
=======================================
// unpack system structure into its constituent elements
DEFUN_DLD(sysout, args, ,
" function sysout(sys[,opt])\n\
  print out a packed system in desired format\n\
 \n\
  sys: packed system\n\
  opt: []: primary system form (default)\n\
      \"ss\": state space form\n\
      \"tf\": transfer function form\n\
      \"zp\": zero-pole form\n\
      \"all\": all of the above")
{
  return sysout(args);
}

octave_value_list sysout(const octave_value_list& args)
{
  octave_value_list retval;

  // This stream is normally connected to the pager.

  int nargin = args.length ();
  if( (nargin < 1) || (nargin > 2) )
    {
      print_usage("sysout:");
      return retval;
    }

  octave_value sysval = args(0);
  if(sysval.is_map())
  {
    cout << "Yes, this is an octave structure." << endl;

    Octave_map map = sysval.map_value();

    octave_value sys_ov = map["sys"];
    if(sys_ov.is_numeric_type() && sys_ov.is_matrix_type() )
    {
      cout << "Yes, sys is a numeric type and a matrix:"
        << sys_ov.rows() << " x " << sys_ov.columns()
        << endl;

      RowVector sys_vec = sys_ov.vector_value();

      for ( int ii = 0 ; ii < sys_vec.length() ; ii++)
        cout << "sys(" << ii << ")=" << sys_vec(ii) << endl;

    }
    else cout << "Nope, sys is not a numeric type, nor a matrix." << endl;
  }
  else
  {
    cout << "No, this is not an octave structure." << endl;
  }

  return retval;

}
--
A S Hodel Assoc. Prof. Dept Elect and Computer Eng, Auburn Univ,AL 36849-5201
On leave at NASA Marshall Space Flight Center (256) 544-1426
Address until 31 July 2000:Mail Code TD-55, MSFC, Alabama, 35812
http://www.eng.auburn.edu/~scotte



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

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



reply via email to

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