Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: beginner with mex

Subject: beginner with mex

From: rahul s

Date: 21 Jul, 2008 23:30:03

Message: 1 of 23

Hi everybody

I have a code which reads a file in fortran and creates a 4
dimensional matrix. The file is in mixed format and I dont
want to recode the thing in matlab, as I think it would be
cumbersome. So I was thinking I could call the fortran as a
mex subroutine and call it from matlab. But since this is a
file reading subroutine, I wanted to know if a mex
subroutine can be written without any inputs that is, prhs
and nrhs.

Regards

Raul

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 01:00:29

Message: 2 of 23

On Jul 22, 11:30=A0am, "rahul s" <m...@yahoo.com> wrote:
> Hi everybody
>
> I have a code which reads a file in fortran and creates a 4
> dimensional matrix. The file is in mixed format and I dont
> want to recode the thing in matlab, as I think it would be
> cumbersome. So I was thinking I could call the fortran as a
> mex subroutine and call it from matlab. But since this is a
> file reading subroutine, I wanted to know if a mex
> subroutine can be written without any inputs that is, prhs
> and nrhs.
>
> Regards
>
> Raul

So, you're going to hardwire the filename into the Fortran routine?
Why not use filename as an input?
That way, later on, you can read a different file with the same
format.

But if you insist on hardwiring the filename into Fortran, then you
can do that OK, with mex having nrhs=3D0, but you need to retain the
subroutine definition:
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
and still define nrhs and prhs as integers.

Subject: Re: beginner with mex

From: James Tursa

Date: 22 Jul, 2008 01:18:03

Message: 3 of 23

NZTideMan <mulgor@gmail.com> wrote in message >
> But if you insist on hardwiring the filename into Fortran,
then you
> can do that OK, with mex having nrhs=3D0

Huh? What does this mean?

James Tursa



Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 05:09:00

Message: 4 of 23

On Jul 22, 1:18=A0pm, "James Tursa" <aclassyguywithakno...@hotmail.com>
wrote:
> NZTideMan <mul...@gmail.com> wrote in message >
> > But if you insist on hardwiring the filename into Fortran,
> then you
> > can do that OK, with mex having nrhs=3D3D0
>
> Huh? What does this mean?
>
> James Tursa

I dunno.
What I typed was nrhs=3Dzero character, and that's the way it appears in
my browser and using Google Groups, but your response has it as 3D0.
I've seen this before in posts.............

Subject: Re: beginner with mex

From: Steve Amphlett

Date: 22 Jul, 2008 08:38:03

Message: 5 of 23

NZTideMan <mulgor@gmail.com> wrote in message <ed5bcb51-
beaf-42f2-a41a-b69ff729a937@r35g2000prm.googlegroups.com>...
> On Jul 22, 1:18=A0pm, "James Tursa"
<aclassyguywithakno...@hotmail.com>
> wrote:
> > NZTideMan <mul...@gmail.com> wrote in message >
> > > But if you insist on hardwiring the filename into
Fortran,
> > then you
> > > can do that OK, with mex having nrhs=3D3D0
> >
> > Huh? What does this mean?
> >
> > James Tursa
>
> I dunno.
> What I typed was nrhs=3Dzero character, and that's the
way it appears in
> my browser and using Google Groups, but your response has
it as 3D0.
> I've seen this before in posts.............

This browser issue is pretty common. Did you type a simple
zero from the top line of the keypad, or something else?

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 09:34:00

Message: 6 of 23

On Jul 22, 8:38=A0pm, "Steve Amphlett" <Firstname.Lastn...@Where-I-
Work.com> wrote:
> NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
>
> beaf-42f2-a41a-b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =
1:18=3DA0pm, "James Tursa"
>
> <aclassyguywithakno...@hotmail.com>
>
>
>
>
>
> > wrote:
> > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > But if you insist on hardwiring the filename into
> Fortran,
> > > then you
> > > > can do that OK, with mex having nrhs=3D3D3D0
>
> > > Huh? What does this mean?
>
> > > James Tursa
>
> > I dunno.
> > What I typed was nrhs=3D3Dzero character, and that's the
> way it appears in
> > my browser and using Google Groups, but your response has
> it as 3D0.
> > I've seen this before in posts.............
>
> This browser issue is pretty common. =A0Did you type a simple
> zero from the top line of the keypad, or something else?- Hide quoted tex=
t -
>
> - Show quoted text -

Yes, I confess. That's what I did. To get a zero, I hit the key on
the top line that has a zero below a right parenthesis on my keyboard.
But hey, why should I apologise? What's wrong with that?

Subject: Re: beginner with mex

From: Steve Amphlett

Date: 22 Jul, 2008 09:52:02

Message: 7 of 23

NZTideMan <mulgor@gmail.com> wrote in message <3645783e-
b716-4ee8-9d6e-634f911e6851@l42g2000hsc.googlegroups.com>...
> On Jul 22, 8:38=A0pm, "Steve Amphlett"
<Firstname.Lastn...@Where-I-
> Work.com> wrote:
> > NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
> >
> > beaf-42f2-a41a-
b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =
> 1:18=3DA0pm, "James Tursa"
> >
> > <aclassyguywithakno...@hotmail.com>
> >
> >
> >
> >
> >
> > > wrote:
> > > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > > But if you insist on hardwiring the filename into
> > Fortran,
> > > > then you
> > > > > can do that OK, with mex having nrhs=3D3D3D0
> >
> > > > Huh? What does this mean?
> >
> > > > James Tursa
> >
> > > I dunno.
> > > What I typed was nrhs=3D3Dzero character, and that's
the
> > way it appears in
> > > my browser and using Google Groups, but your response
has
> > it as 3D0.
> > > I've seen this before in posts.............
> >
> > This browser issue is pretty common. =A0Did you type a
simple
> > zero from the top line of the keypad, or something
else?- Hide quoted tex=
> t -
> >
> > - Show quoted text -
>
> Yes, I confess. That's what I did. To get a zero, I hit
the key on
> the top line that has a zero below a right parenthesis on
my keyboard.
> But hey, why should I apologise? What's wrong with that?

Odd. Note how the quoted text above is gradually getting
more mangled too. How do you post?

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 10:06:08

Message: 8 of 23

On Jul 22, 9:52=A0pm, "Steve Amphlett" <Firstname.Lastn...@Where-I-
Work.com> wrote:
> NZTideMan <mul...@gmail.com> wrote in message <3645783e-
>
> b716-4ee8-9d6e-634f911e6...@l42g2000hsc.googlegroups.com>...> On Jul 22, =
8:38=3DA0pm, "Steve Amphlett"
>
> <Firstname.Lastn...@Where-I-> Work.com> wrote:
> > > NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
>
> > > beaf-42f2-a41a-
>
> b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =3D
>
>
>
> > 1:18=3D3DA0pm, "James Tursa"
>
> > > <aclassyguywithakno...@hotmail.com>
>
> > > > wrote:
> > > > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > > > But if you insist on hardwiring the filename into
> > > Fortran,
> > > > > then you
> > > > > > can do that OK, with mex having nrhs=3D3D3D3D0
>
> > > > > Huh? What does this mean?
>
> > > > > James Tursa
>
> > > > I dunno.
> > > > What I typed was nrhs=3D3D3Dzero character, and that's
> the
> > > way it appears in
> > > > my browser and using Google Groups, but your response
> has
> > > it as 3D0.
> > > > I've seen this before in posts.............
>
> > > This browser issue is pretty common. =3DA0Did you type a
> simple
> > > zero from the top line of the keypad, or something
>
> else?- Hide quoted tex=3D
>
> > t -
>
> > > - Show quoted text -
>
> > Yes, I confess. =A0That's what I did. =A0To get a zero, I hit
> the key on
> > the top line that has a zero below a right parenthesis on
> my keyboard.
> > But hey, why should I apologise? =A0What's wrong with that?
>
> Odd. =A0Note how the quoted text above is gradually getting
> more mangled too. =A0How do you post?- Hide quoted text -
>
> - Show quoted text -

I'm in Google Groups. My browser is Maxthon.

Subject: Re: beginner with mex

From: Rune Allnor

Date: 22 Jul, 2008 10:11:26

Message: 9 of 23

On 22 Jul, 01:30, "rahul s" <m...@yahoo.com> wrote:
> Hi everybody
>
> I have a code which reads a file in fortran and creates a 4
> dimensional matrix. The file is in mixed format and I dont
> want to recode the thing in matlab, as I think it would be
> cumbersome. So I was thinking I could call the fortran as a
> mex subroutine and call it from matlab. But since this is a
> file reading subroutine, I wanted to know if a mex
> subroutine can be written without any inputs that is, prhs
> and nrhs.

The call of MEXfunction needs to contain all those
variables you find in the docs (I use C and C++,
not fortran, so I don't know the details).

So you can not call MEXfunction in any other way than
specified. Of course, you may call the mex'ed function
from matlab without arguments, meaning that nrhs == 0
at the point of entry in MEXfunction.

One useful way to merge old code and MEX is to use
MEXfunction as, well, an interface to the old code.
This means that you just reformat any arguments
provided from matlab to whatever format is required
by the old code, run the old code as function calls
from MEXfunction, then reformat the results to the
formats of the returned variables to matlab.

If the fortran (or C or C++) code is well designed,
more than 90% of the work of writing MEXfunction
has to do with argument validation and reformatting
of arguments and data.

Rune

Subject: Re: beginner with mex

From: Peter Boettcher

Date: 22 Jul, 2008 13:30:27

Message: 10 of 23

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> writes:

> NZTideMan <mulgor@gmail.com> wrote in message <ed5bcb51-
> beaf-42f2-a41a-b69ff729a937@r35g2000prm.googlegroups.com>...
>> On Jul 22, 1:18=A0pm, "James Tursa"
> <aclassyguywithakno...@hotmail.com>
>> wrote:
>> > NZTideMan <mul...@gmail.com> wrote in message >
>> > > But if you insist on hardwiring the filename into Fortran, then
>> > > you can do that OK, with mex having nrhs=3D3D0
>> >
>> > Huh? What does this mean?
>> >
>> > James Tursa
>>
>> I dunno. What I typed was nrhs=3Dzero character, and that's the way
>> it appears in my browser and using Google Groups, but your response
>> has it as 3D0. I've seen this before in posts.............
>
> This browser issue is pretty common. Did you type a simple
> zero from the top line of the keypad, or something else?

NZTideMan's post (from Google Groups) is encoded in quoted-printable.
This is not a standard for USENET, but lots of newsreaders support it.
Thunderbird and GNUS, for example, see the 0. Google Groups could be
blamed for that. On the other hand, since TMW pretends to run a USENET
interface, their web interface could easily pay attention to
Content-Transfer-Encoding and do the right thing with it.

-Peter

Subject: Re: beginner with mex

From: rahul s

Date: 22 Jul, 2008 20:45:06

Message: 11 of 23

Rune Allnor <allnor@tele.ntnu.no> wrote in message
<39a090e4-bb52-4de1-ae14-008c57d3b5d8@m36g2000hse.googlegroups.com>...
> On 22 Jul, 01:30, "rahul s" <m...@yahoo.com> wrote:
> > Hi everybody
> >
> > I have a code which reads a file in fortran and creates a 4
> > dimensional matrix. The file is in mixed format and I dont
> > want to recode the thing in matlab, as I think it would be
> > cumbersome. So I was thinking I could call the fortran as a
> > mex subroutine and call it from matlab. But since this is a
> > file reading subroutine, I wanted to know if a mex
> > subroutine can be written without any inputs that is, prhs
> > and nrhs.
>

Thanks a lot everyone, for the prompt reply. But I now face
another problem. I could manage to run the mex without any
input, but the issue is with the output 4 dimensional matrix
I create from the file which is passed onto matlab. I dont
know the prior size of the matrix. I only know it is a 4
Dimensional one. So when I create output matrix,
plhs(1)= mxCreateCellArray(ndim, dims)
ndim=4, what do I put for dims?
I know the value of dims only from the file.It is something
like an allocatable array.
Any help is greatly appreciated.

Regards
Rahul S


> The call of MEXfunction needs to contain all those
> variables you find in the docs (I use C and C++,
> not fortran, so I don't know the details).
>
> So you can not call MEXfunction in any other way than
> specified. Of course, you may call the mex'ed function
> from matlab without arguments, meaning that nrhs == 0
> at the point of entry in MEXfunction.
>
> One useful way to merge old code and MEX is to use
> MEXfunction as, well, an interface to the old code.
> This means that you just reformat any arguments
> provided from matlab to whatever format is required
> by the old code, run the old code as function calls
> from MEXfunction, then reformat the results to the
> formats of the returned variables to matlab.
>
> If the fortran (or C or C++) code is well designed,
> more than 90% of the work of writing MEXfunction
> has to do with argument validation and reformatting
> of arguments and data.
>
> Rune

Subject: Re: beginner with mex

From: Rune Allnor

Date: 22 Jul, 2008 21:55:00

Message: 12 of 23

On 22 Jul, 22:45, "rahul s" <m...@yahoo.com> wrote:
> I could manage to run the mex without any
> input, but the issue is with the output 4 dimensional matrix
> I create from the file which is passed onto matlab. I dont
> know the prior size of the matrix. I only know it is a 4
> Dimensional one. So when I create output matrix,
> plhs(1)= mxCreateCellArray(ndim, dims)
> ndim=4, what do I put for dims?
> I know the value of dims only from the file.

You need to scan the file for the dimensions first, and find
the dimensions you need to know. Once you know the dimensions
you allocate the array.

How to actually do this depends a bit on what style you
program in. If you use object oriented programming you
just store the dimensions as member variables in some
object.

If you use procedural programing you might want to
rewrite the program to include the dimensions among
the parameters returned from the main function.
The easiest way might be to scan the file for the
dimensions before you call your present routines.
This will be a quick fix on your part, but it will
make your program run a lot slower.

Rune

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 01:00:29

Message: 13 of 23

On Jul 22, 11:30=A0am, "rahul s" <m...@yahoo.com> wrote:
> Hi everybody
>
> I have a code which reads a file in fortran and creates a 4
> dimensional matrix. The file is in mixed format and I dont
> want to recode the thing in matlab, as I think it would be
> cumbersome. So I was thinking I could call the fortran as a
> mex subroutine and call it from matlab. But since this is a
> file reading subroutine, I wanted to know if a mex
> subroutine can be written without any inputs that is, prhs
> and nrhs.
>
> Regards
>
> Raul

So, you're going to hardwire the filename into the Fortran routine?
Why not use filename as an input?
That way, later on, you can read a different file with the same
format.

But if you insist on hardwiring the filename into Fortran, then you
can do that OK, with mex having nrhs=3D0, but you need to retain the
subroutine definition:
subroutine mexFunction(nlhs,plhs,nrhs,prhs)
and still define nrhs and prhs as integers.

Subject: Re: beginner with mex

From: James Tursa

Date: 22 Jul, 2008 01:18:03

Message: 14 of 23

NZTideMan <mulgor@gmail.com> wrote in message >
> But if you insist on hardwiring the filename into Fortran,
then you
> can do that OK, with mex having nrhs=3D0

Huh? What does this mean?

James Tursa



Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 05:09:00

Message: 15 of 23

On Jul 22, 1:18=A0pm, "James Tursa" <aclassyguywithakno...@hotmail.com>
wrote:
> NZTideMan <mul...@gmail.com> wrote in message >
> > But if you insist on hardwiring the filename into Fortran,
> then you
> > can do that OK, with mex having nrhs=3D3D0
>
> Huh? What does this mean?
>
> James Tursa

I dunno.
What I typed was nrhs=3Dzero character, and that's the way it appears in
my browser and using Google Groups, but your response has it as 3D0.
I've seen this before in posts.............

Subject: Re: beginner with mex

From: Steve Amphlett

Date: 22 Jul, 2008 08:38:03

Message: 16 of 23

NZTideMan <mulgor@gmail.com> wrote in message <ed5bcb51-
beaf-42f2-a41a-b69ff729a937@r35g2000prm.googlegroups.com>...
> On Jul 22, 1:18=A0pm, "James Tursa"
<aclassyguywithakno...@hotmail.com>
> wrote:
> > NZTideMan <mul...@gmail.com> wrote in message >
> > > But if you insist on hardwiring the filename into
Fortran,
> > then you
> > > can do that OK, with mex having nrhs=3D3D0
> >
> > Huh? What does this mean?
> >
> > James Tursa
>
> I dunno.
> What I typed was nrhs=3Dzero character, and that's the
way it appears in
> my browser and using Google Groups, but your response has
it as 3D0.
> I've seen this before in posts.............

This browser issue is pretty common. Did you type a simple
zero from the top line of the keypad, or something else?

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 09:34:00

Message: 17 of 23

On Jul 22, 8:38=A0pm, "Steve Amphlett" <Firstname.Lastn...@Where-I-
Work.com> wrote:
> NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
>
> beaf-42f2-a41a-b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =
1:18=3DA0pm, "James Tursa"
>
> <aclassyguywithakno...@hotmail.com>
>
>
>
>
>
> > wrote:
> > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > But if you insist on hardwiring the filename into
> Fortran,
> > > then you
> > > > can do that OK, with mex having nrhs=3D3D3D0
>
> > > Huh? What does this mean?
>
> > > James Tursa
>
> > I dunno.
> > What I typed was nrhs=3D3Dzero character, and that's the
> way it appears in
> > my browser and using Google Groups, but your response has
> it as 3D0.
> > I've seen this before in posts.............
>
> This browser issue is pretty common. =A0Did you type a simple
> zero from the top line of the keypad, or something else?- Hide quoted tex=
t -
>
> - Show quoted text -

Yes, I confess. That's what I did. To get a zero, I hit the key on
the top line that has a zero below a right parenthesis on my keyboard.
But hey, why should I apologise? What's wrong with that?

Subject: Re: beginner with mex

From: Steve Amphlett

Date: 22 Jul, 2008 09:52:02

Message: 18 of 23

NZTideMan <mulgor@gmail.com> wrote in message <3645783e-
b716-4ee8-9d6e-634f911e6851@l42g2000hsc.googlegroups.com>...
> On Jul 22, 8:38=A0pm, "Steve Amphlett"
<Firstname.Lastn...@Where-I-
> Work.com> wrote:
> > NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
> >
> > beaf-42f2-a41a-
b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =
> 1:18=3DA0pm, "James Tursa"
> >
> > <aclassyguywithakno...@hotmail.com>
> >
> >
> >
> >
> >
> > > wrote:
> > > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > > But if you insist on hardwiring the filename into
> > Fortran,
> > > > then you
> > > > > can do that OK, with mex having nrhs=3D3D3D0
> >
> > > > Huh? What does this mean?
> >
> > > > James Tursa
> >
> > > I dunno.
> > > What I typed was nrhs=3D3Dzero character, and that's
the
> > way it appears in
> > > my browser and using Google Groups, but your response
has
> > it as 3D0.
> > > I've seen this before in posts.............
> >
> > This browser issue is pretty common. =A0Did you type a
simple
> > zero from the top line of the keypad, or something
else?- Hide quoted tex=
> t -
> >
> > - Show quoted text -
>
> Yes, I confess. That's what I did. To get a zero, I hit
the key on
> the top line that has a zero below a right parenthesis on
my keyboard.
> But hey, why should I apologise? What's wrong with that?

Odd. Note how the quoted text above is gradually getting
more mangled too. How do you post?

Subject: Re: beginner with mex

From: NZTideMan

Date: 22 Jul, 2008 10:06:08

Message: 19 of 23

On Jul 22, 9:52=A0pm, "Steve Amphlett" <Firstname.Lastn...@Where-I-
Work.com> wrote:
> NZTideMan <mul...@gmail.com> wrote in message <3645783e-
>
> b716-4ee8-9d6e-634f911e6...@l42g2000hsc.googlegroups.com>...> On Jul 22, =
8:38=3DA0pm, "Steve Amphlett"
>
> <Firstname.Lastn...@Where-I-> Work.com> wrote:
> > > NZTideMan <mul...@gmail.com> wrote in message <ed5bcb51-
>
> > > beaf-42f2-a41a-
>
> b69ff729a...@r35g2000prm.googlegroups.com>...> On Jul 22, =3D
>
>
>
> > 1:18=3D3DA0pm, "James Tursa"
>
> > > <aclassyguywithakno...@hotmail.com>
>
> > > > wrote:
> > > > > NZTideMan <mul...@gmail.com> wrote in message >
> > > > > > But if you insist on hardwiring the filename into
> > > Fortran,
> > > > > then you
> > > > > > can do that OK, with mex having nrhs=3D3D3D3D0
>
> > > > > Huh? What does this mean?
>
> > > > > James Tursa
>
> > > > I dunno.
> > > > What I typed was nrhs=3D3D3Dzero character, and that's
> the
> > > way it appears in
> > > > my browser and using Google Groups, but your response
> has
> > > it as 3D0.
> > > > I've seen this before in posts.............
>
> > > This browser issue is pretty common. =3DA0Did you type a
> simple
> > > zero from the top line of the keypad, or something
>
> else?- Hide quoted tex=3D
>
> > t -
>
> > > - Show quoted text -
>
> > Yes, I confess. =A0That's what I did. =A0To get a zero, I hit
> the key on
> > the top line that has a zero below a right parenthesis on
> my keyboard.
> > But hey, why should I apologise? =A0What's wrong with that?
>
> Odd. =A0Note how the quoted text above is gradually getting
> more mangled too. =A0How do you post?- Hide quoted text -
>
> - Show quoted text -

I'm in Google Groups. My browser is Maxthon.

Subject: Re: beginner with mex

From: Rune Allnor

Date: 22 Jul, 2008 10:11:26

Message: 20 of 23

On 22 Jul, 01:30, "rahul s" <m...@yahoo.com> wrote:
> Hi everybody
>
> I have a code which reads a file in fortran and creates a 4
> dimensional matrix. The file is in mixed format and I dont
> want to recode the thing in matlab, as I think it would be
> cumbersome. So I was thinking I could call the fortran as a
> mex subroutine and call it from matlab. But since this is a
> file reading subroutine, I wanted to know if a mex
> subroutine can be written without any inputs that is, prhs
> and nrhs.

The call of MEXfunction needs to contain all those
variables you find in the docs (I use C and C++,
not fortran, so I don't know the details).

So you can not call MEXfunction in any other way than
specified. Of course, you may call the mex'ed function
from matlab without arguments, meaning that nrhs == 0
at the point of entry in MEXfunction.

One useful way to merge old code and MEX is to use
MEXfunction as, well, an interface to the old code.
This means that you just reformat any arguments
provided from matlab to whatever format is required
by the old code, run the old code as function calls
from MEXfunction, then reformat the results to the
formats of the returned variables to matlab.

If the fortran (or C or C++) code is well designed,
more than 90% of the work of writing MEXfunction
has to do with argument validation and reformatting
of arguments and data.

Rune

Subject: Re: beginner with mex

From: Peter Boettcher

Date: 22 Jul, 2008 13:30:27

Message: 21 of 23

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com> writes:

> NZTideMan <mulgor@gmail.com> wrote in message <ed5bcb51-
> beaf-42f2-a41a-b69ff729a937@r35g2000prm.googlegroups.com>...
>> On Jul 22, 1:18=A0pm, "James Tursa"
> <aclassyguywithakno...@hotmail.com>
>> wrote:
>> > NZTideMan <mul...@gmail.com> wrote in message >
>> > > But if you insist on hardwiring the filename into Fortran, then
>> > > you can do that OK, with mex having nrhs=3D3D0
>> >
>> > Huh? What does this mean?
>> >
>> > James Tursa
>>
>> I dunno. What I typed was nrhs=3Dzero character, and that's the way
>> it appears in my browser and using Google Groups, but your response
>> has it as 3D0. I've seen this before in posts.............
>
> This browser issue is pretty common. Did you type a simple
> zero from the top line of the keypad, or something else?

NZTideMan's post (from Google Groups) is encoded in quoted-printable.
This is not a standard for USENET, but lots of newsreaders support it.
Thunderbird and GNUS, for example, see the 0. Google Groups could be
blamed for that. On the other hand, since TMW pretends to run a USENET
interface, their web interface could easily pay attention to
Content-Transfer-Encoding and do the right thing with it.

-Peter

Subject: Re: beginner with mex

From: rahul s

Date: 22 Jul, 2008 20:45:06

Message: 22 of 23

Rune Allnor <allnor@tele.ntnu.no> wrote in message
<39a090e4-bb52-4de1-ae14-008c57d3b5d8@m36g2000hse.googlegroups.com>...
> On 22 Jul, 01:30, "rahul s" <m...@yahoo.com> wrote:
> > Hi everybody
> >
> > I have a code which reads a file in fortran and creates a 4
> > dimensional matrix. The file is in mixed format and I dont
> > want to recode the thing in matlab, as I think it would be
> > cumbersome. So I was thinking I could call the fortran as a
> > mex subroutine and call it from matlab. But since this is a
> > file reading subroutine, I wanted to know if a mex
> > subroutine can be written without any inputs that is, prhs
> > and nrhs.
>

Thanks a lot everyone, for the prompt reply. But I now face
another problem. I could manage to run the mex without any
input, but the issue is with the output 4 dimensional matrix
I create from the file which is passed onto matlab. I dont
know the prior size of the matrix. I only know it is a 4
Dimensional one. So when I create output matrix,
plhs(1)= mxCreateCellArray(ndim, dims)
ndim=4, what do I put for dims?
I know the value of dims only from the file.It is something
like an allocatable array.
Any help is greatly appreciated.

Regards
Rahul S


> The call of MEXfunction needs to contain all those
> variables you find in the docs (I use C and C++,
> not fortran, so I don't know the details).
>
> So you can not call MEXfunction in any other way than
> specified. Of course, you may call the mex'ed function
> from matlab without arguments, meaning that nrhs == 0
> at the point of entry in MEXfunction.
>
> One useful way to merge old code and MEX is to use
> MEXfunction as, well, an interface to the old code.
> This means that you just reformat any arguments
> provided from matlab to whatever format is required
> by the old code, run the old code as function calls
> from MEXfunction, then reformat the results to the
> formats of the returned variables to matlab.
>
> If the fortran (or C or C++) code is well designed,
> more than 90% of the work of writing MEXfunction
> has to do with argument validation and reformatting
> of arguments and data.
>
> Rune

Subject: Re: beginner with mex

From: Rune Allnor

Date: 22 Jul, 2008 21:55:00

Message: 23 of 23

On 22 Jul, 22:45, "rahul s" <m...@yahoo.com> wrote:
> I could manage to run the mex without any
> input, but the issue is with the output 4 dimensional matrix
> I create from the file which is passed onto matlab. I dont
> know the prior size of the matrix. I only know it is a 4
> Dimensional one. So when I create output matrix,
> plhs(1)= mxCreateCellArray(ndim, dims)
> ndim=4, what do I put for dims?
> I know the value of dims only from the file.

You need to scan the file for the dimensions first, and find
the dimensions you need to know. Once you know the dimensions
you allocate the array.

How to actually do this depends a bit on what style you
program in. If you use object oriented programming you
just store the dimensions as member variables in some
object.

If you use procedural programing you might want to
rewrite the program to include the dimensions among
the parameters returned from the main function.
The easiest way might be to scan the file for the
dimensions before you call your present routines.
This will be a quick fix on your part, but it will
make your program run a lot slower.

Rune

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
mex fortran rahul s 22 Jul, 2008 16:45:13
mex rahul s 21 Jul, 2008 19:30:22
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics