help-octave
[Top][All Lists]
Advanced

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

Re: Problem with mexPutVariable


From: Michael Goffioul
Subject: Re: Problem with mexPutVariable
Date: Wed, 22 May 2013 13:49:57 -0400

On Wed, May 22, 2013 at 12:18 PM, DONATI, KORY D CTR USAF AFSPC WROCI/InDyne <address@hidden> wrote:
Hello all,
I asked a similar question previously that went unanswered. I have dug into my problem more and have found what I think to be the root cause: mexPutVariable. I am still new to using mex files and their language, so I might not have my code correct.

I am trying to put an mxarray into the caller workspace for use within an octave function. However, it appears that when the option for "caller" is selected in mexPutVariable, the array is passed to "base". Here is a simple mex source file I made to illustrate my problem. This file compiles and works as expected in matlab ver 7.0.6.0. Also included is the .m file I used to test to make sure if the caller option in mexPutVariable was working.


/* start: simple_array.c*/
#include"mex.h"
/* Include matrix.h if using matlab.*/
/* #include"matrix.h"*/
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{

/* Create an array of double and an mxarray and a pointer*/
double arr1[5];
mxArray  *array_ptr = NULL;
/* Set up array Elements*/
arr1[0] = 2;
arr1[1] = 4;
arr1[2] = 6;
arr1[3] = 8;
arr1[4] = 10;


/* Create an mxArray. */
   array_ptr = mxCreateDoubleMatrix(1, 5, mxREAL);

   /*Set array to the mxarray*/
   mxSetPr(array_ptr, arr1);

   /*Pass mxarray to caller workspace... NOT WORKING */
   mexPutVariable ("caller","out_caller",array_ptr);

   /*Pass mxarray to base workspace*/
   mexPutVariable ("base","out_base",array_ptr);

   /*Pass mxarray to global workspace*/
   mexPutVariable ("global","out_global",array_ptr);

   }

/* end: simple_array.c*/

********************************

% START:run_me.m
function run_me()

simple_array()
whos

% This line returns an error in octave, but not matlab.
display(out_caller)

end
% END: run_me.m

_______________________________________________
Help-octave mailing list
address@hidden
https://mailman.cae.wisc.edu/listinfo/help-octave

This looks like a bug. Please make a bug report about it. For the record, it appears the variable is put into the scope of the caller of "run_me", in this case the base scope. So it basically skips the first frame in the call stack. If you put another function in between, like the following:

function run_me()
simple_array()
end

function run_me2()
run_me()
display(out_caller)
end

You'll see that the variable appears in run_me2 scope.

When scanning the octave code base, the only place where I can find the problematic code is for "source" implementation, such that you can call "source('filename', 'caller')". In this case, the top of the call stack is always the "source" function. And using "caller" as argument means you want to source the file in the caller of the currently executing function, not the caller of "source" (which is the currently executing function). In other words, skipping the first frame of the call stack makes sense.

Michael.


reply via email to

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