That's what I was afraid of, but I am definitely open to
any suggestions.

Your suggestion worked.  I was successful converting a perl
array ref to a C array and from that I was able to directly
plot the points on a matlab graph.

Just in case you're curious, here is my code:

sub MLlineplot {
   my ($data_ref) = @_;
   my ($elsX,$elsY);
   
   # Parse the data_ref
   $elsX = @{$data_ref->{x}};
   $elsY = @{$data_ref->{y}};
   
   unless ($elsX == $elsY) {
      print STDERR "ERROR:  Vectors x and y do not match.  They must have
the".
                   " same number of elements.\n\n";
      pprint($data_ref);
      die;  
   }
   
   cLinePlot($data_ref->{x},$data_ref->{y},$elsX);

} #end MLplot

/*
 * cLinePlot() uses pointplot.m to plot a simple line graph
 *
 * Arguments:
 *   SV* x   -  scalar value pointer to the x values array
 *   SV* y   -  scalar value pointer to the y values array
 *   els     -  integer number of elements in arrays x and y
 *   
 * Note:  Error handling for data and options is done in MLplot()
 *        found above.
*/
void cLinePlot(SV* x, SV* y, int els) {
   
   int i;
   SV* tmpX;
   SV* tmpY;
   double tmpXN; 
   double tmpYN;
   mxArray * X = NULL;
   mxArray * Y = NULL;
   AV *arrX = (AV*) SvRV(x);
   AV *arrY = (AV*) SvRV(y);
   double dataX[els]; 
   double dataY[els];

   for (i=0; i < els; i++) {
   
      // pull first element off array
      tmpX = av_shift(arrX);
      tmpY = av_shift(arrY);

      // Convert the SV element to a double
      tmpXN = SvNV(tmpX);
      tmpYN = SvNV(tmpY);
      
      dataX[i] = tmpXN;
      dataY[i] = tmpYN;
      
      //printf("%g\n", dataX[i]);
   }   
   
   // Bind the data stored in data*[] to X or Y(mxArrays)
   mlfAssign(&X, mlfDoubleMatrix(1, els, dataX, 0));   
   mlfAssign(&Y, mlfDoubleMatrix(1, els, dataY, 0));
      
   libpointplotInitialize();  /* Initialize the library of matlab
M-Functions */

   /* Call mlfPointPlot, the compiled version of pointplot.m. */
   mlfPointplot(X,Y);
    
   libpointplotTerminate();    /* Terminate the library of matlab
M-functions */  
   
   mxDestroyArray(X);
   mxDestroyArray(Y);

}

Thanks again...Brady

-----Original Message-----
From: Neil Watkiss [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 10, 2002 2:49 PM
To: bbcannon
Cc: 'Elizabeth Mattijsen'; '[EMAIL PROTECTED]'
Subject: Re: Instantiating arrays with Inline


bbcannon [10/06/02 14:43 -0600]:
> That sounds like a great idea if you know a
> more efficient way of creating an array from this string
> reference than Neil showed with his for loop.
> I'm very new to C, can you show me how to do this?

It seems to me that either way you'll be iterating through thousands of
entries. You might save a bit of overhead not going through an AV*, but I
suggest trying it first. Before using a complicated solution, you might as
well see whether the simple one is good enough. You're already in C, which
is
very fast. Otherwise you're going to be taking a Perl array, using join() to
get a string, then basically splitting it up again from C into a C array. It
strikes me that this solution might be _slower_, since you could avoid the
join() and the split() by just converting the Perl array into a C array
directly.

Later,
Neil

Reply via email to