Thread Subject: Calling binary reading m-script from .c exe file (mex)

Subject: Calling binary reading m-script from .c exe file (mex)

From: Lars Christiansen

Date: 18 Oct, 2007 13:50:41

Message: 1 of 5

Hi!


Background:
-----------
I have a binary data file created by MODFLOW (a groundwater
modelling software), which I need to extract data from. I
do not know the exact structure of the file, but MATLAB's
fread function reads it anyways.

The problem is that I need to read the data over and over
again, invoking the process each time from the Windows
command prompt, as part of a calibration routine running
under the Windows prompt.

In order to only have one MATLAB engine running, my idéa is
to do this through an executable file made in C (or C++),
compiled using the mex function.

A have based my script on the example file "engwindemo.c"
from MATLAB's Help, so I know that the basics work (engine
start up, passing simple commands like "A = 2 + 4;", etc.).


Problem/Question:
-----------------
I have not been able to make fread work as part of my
c-file. Basically, nothing happens. Does mex not support
a call to certain MATLAB functions, like fread? Is there a
list of supported functions somewhere (I have looked).

Ideally, I would like to have a simple c-code program, which
just passes a call to a MATLAB script that executes as if it
was called from the MATLAB prompt. E.g.:

    engEvalString(ep, "plotter.m");

should equal

    >> plotter

I have attacked my "reading the binary data"-problem in many
different ways, but I am running out of idéas now, so I
really hope one of you guys can help me!

Cheers, Lars

Subject: Calling binary reading m-script from .c exe file (mex)

From: Lars Christiansen

Date: 19 Oct, 2007 10:16:46

Message: 2 of 5

"Lars Christiansen" <lac@er.dtu.dk> wrote in message
<ff7obh$ku8$1@fred.mathworks.com>...
> Hi!
>

Hi again!

Problem solved (with a little help from a friend).

I needed to (1) set _absolute_ paths in my m-scripts and (2)
include the m-script I call from the c-code executable file
in the MATLAB paths. Reason: when the MATLAB engine starts
up, it does _not_ start up in the present working directory.

In this way, I now have a simple exe-file which calls a
m-script that takes care of all the rest for me.

Have a nice weekend!
Lars

Subject: Calling binary reading m-script from .c exe file (mex)

From: James Tursa

Date: 26 Oct, 2007 07:38:52

Message: 3 of 5

On Fri, 19 Oct 2007 10:16:46 +0000 (UTC), "Lars Christiansen"
<lac@er.dtu.dk> wrote:

>"Lars Christiansen" <lac@er.dtu.dk> wrote in message
><ff7obh$ku8$1@fred.mathworks.com>...
>> Hi!
>>
>
>Hi again!
>
>Problem solved (with a little help from a friend).
>
>I needed to (1) set _absolute_ paths in my m-scripts and (2)
>include the m-script I call from the c-code executable file
>in the MATLAB paths. Reason: when the MATLAB engine starts
>up, it does _not_ start up in the present working directory.
>
>In this way, I now have a simple exe-file which calls a
>m-script that takes care of all the rest for me.
>
>Have a nice weekend!
>Lars

Another solution, which I employ in all of my engine applications, is
to set the working directory of the MATLAB engine automatically right
after it is opened from within the c-code. I use getcwd(cwd) in the
c-code, construct another string with "cd '"+cwd+"';", and then call
engEvalString with this command. That gets the engine pointed to the
c-code directory regardless of where the c-code is running from. I
also use chdir(cwd) and another engEvalString call to change working
directories and still force the engine to point to the c-code working
directory.

James Tursa

Subject: Calling binary reading m-script from .c exe file (mex)

From: mustafa ahmed

Date: 26 Nov, 2009 11:22:04

Message: 4 of 5

Hi Lars and everybody ,
Do you have a program on matalb or any on language to read the MODFLOW output files (cell by cell file, as binary type)? I can read the cell by cell budget binary file in ZoneBudget, MODPATH, GW Chart and ModelMuse. But I need to read and imported in matlab to do some calculation in matlab.
Thank you in advance!
best regards,
Mustafa


"Lars Christiansen" <lac@er.dtu.dk> wrote in message <ffa06e$537$1@fred.mathworks.com>...
> "Lars Christiansen" <lac@er.dtu.dk> wrote in message
> <ff7obh$ku8$1@fred.mathworks.com>...
> > Hi!
> >
>
> Hi again!
>
> Problem solved (with a little help from a friend).
>
> I needed to (1) set _absolute_ paths in my m-scripts and (2)
> include the m-script I call from the c-code executable file
> in the MATLAB paths. Reason: when the MATLAB engine starts
> up, it does _not_ start up in the present working directory.
>
> In this way, I now have a simple exe-file which calls a
> m-script that takes care of all the rest for me.
>
> Have a nice weekend!
> Lars

Subject: Calling binary reading m-script from .c exe file (mex)

From: Jim N.

Date: 15 Jan, 2010 09:25:06

Message: 5 of 5

> Another solution, which I employ in all of my engine applications, is
> to set the working directory of the MATLAB engine automatically right
> after it is opened from within the c-code. I use getcwd(cwd) in the
> c-code, construct another string with "cd '"+cwd+"';", and then call
> engEvalString with this command. That gets the engine pointed to the
> c-code directory regardless of where the c-code is running from. I
> also use chdir(cwd) and another engEvalString call to change working
> directories and still force the engine to point to the c-code working
> directory.

So I tried this procedure but with no luck. I still get the error message
"??? Undefined function or variable 'myFunc'." The engine can't find my custom function in a m-file existing in the set working directory.

I'm using Mac OS and Matlab 2008. Are there any buggs known? Can this be reported to work?

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
working directory Jim N. 15 Jan, 2010 04:29:06
chdir Jim N. 15 Jan, 2010 04:29:06
cd Jim N. 15 Jan, 2010 04:29:06
path Jim N. 15 Jan, 2010 04:29:06
bug Jim N. 15 Jan, 2010 04:29:06
c Jim N. 15 Jan, 2010 04:29:06
engine Jim N. 15 Jan, 2010 04:29:06
binary data Lars Christiansen 18 Oct, 2007 09:55:06
engine Lars Christiansen 18 Oct, 2007 09:55:06
fread Lars Christiansen 18 Oct, 2007 09:55:06
mex Lars Christiansen 18 Oct, 2007 09:55:06
rssFeed for this Thread

Contact us at files@mathworks.com