From: Owen Mead-Robins on
Hello Matlab experts.

Over the past few days I've been fighting with Matlab and global
variables. What I'm trying to do in a Nutshell:

1) Read in data from a MatLab MAT-file containing matlab variables
2) Use those variables in a compiled matlab function
- This needs to run stupidly fast, so using plhs[] and prhs[] won't
work (they copy data)
- Modification to the variables in the loop need to be available to
the next iteration

I've included the C code as well as the M file which hopefully will
give some better context. I've stripped out a lot to make it simple
to read and only included one variable.

My question: The only way I could get the compiled loop to use a
global variable was to use the mclInitializeDoubleVector function. It
works, but makes for some really ugly code. Is there a better way to
get a compiled function to use a global variable? I think I'm missing
something critical here.

-Owen
(Using Matlab 6.5 and its compiler)

mexPrintf("%s:
%s\n",mxGetFieldNameByNumber(plhs[0],j),mxArrayToString(mxGetFieldByNu
mber(plhs[0],0,j)));

// -------------------------------------- The C Code
-------------------------------------- //

#include "libmatlb.h"
#include "theLoop.h"

mxArray *refMeasurements = NULL;

int main(int argc, char* argv[])
{
InitializeModule_theLoop();

// Read in the variables from the MAT-file
// A compiled wrapper of the Matlab "load" function to retrieve a
specified variable
refMeasurements= mlfLoadAOVar(mxCreateString("refMeasurements" ));

// Is this really needed????
double *curMxArray = mxGetPr(refMeasurements);
refMeasurements = mclInitializeDoubleVector(mxGetM(refMeasurements),
mxGetN(refMeasurements), curMxArray);

while(untilUserQuit)
mlfTheLoop(); // Call the compiled matlab function

TerminateModule_theLoop();
}

// -------------------------------------- The M File
-------------------------------------- //
function theLoop()
global refMeasurements;

% Perform Magic Here, modifying refMeasurements and others for next
loop
 | 
Pages: 1
Prev: Matlab Com-VB interaction
Next: matlab project