From: Guilherme Rocha on
I am a bit confused about the documentation in
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f33356.html#f15433

The documentation directs us to "use the mxCreate functions to create the MATLAB arrays for [our] output argument".
However, .
Since there is not a specific mxCreate function to create an array of integers,
what should I do to allocate space for a matrix of integers?

Should I just create the array of integers using mxCreateDoubleArray and let mxCopyInteger4ToPtr deal with type conversions?

In other words: does the "double" in mxCreateDoubleArray refer to the type of variable (integer, float, double) or does it mean that the array is 2-dimensional (aka matrix).

Inicidental question: if the return value is a scalar, do I need to create a 1x1 array using mxCreateDoubleArray or is there a better way to deal with that?

Thanks for any help,

G.
From: James Tursa on
"Guilherme Rocha" <gvrocha(a)gmail.com> wrote in message <i43njn$r3a$1(a)fred.mathworks.com>...
> I am a bit confused about the documentation in
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f33356.html#f15433
>
> The documentation directs us to "use the mxCreate functions to create the MATLAB arrays for [our] output argument".
> However, .
> Since there is not a specific mxCreate function to create an array of integers,
> what should I do to allocate space for a matrix of integers?

By "integers" I assume you mean integer type variables, not double variables with integral values. You can use mxCreateNumericMatrix or mxCreateNumericArray for this.

> Should I just create the array of integers using mxCreateDoubleArray and let mxCopyInteger4ToPtr deal with type conversions?

No. That will give the wrong class type to the variable.

> In other words: does the "double" in mxCreateDoubleArray refer to the type of variable (integer, float, double) or does it mean that the array is 2-dimensional (aka matrix).

No. The "double" refers to the class of the MATLAB variable ... it has nothing to do with the size or dimensions of the variable.

> Inicidental question: if the return value is a scalar, do I need to create a 1x1 array using mxCreateDoubleArray or is there a better way to deal with that?

If you want to return a double scalar, use mxCreateDoubleScalar. If you want to return any other type of scalar (e.g., int32), then you will have to use mxCreateNumericMatrix along with mxGetData and appropriate mxCopy___ routines to create the mxArray scalar.


James Tursa
From: Guilherme Rocha on
Thanks, that explains a lot.

And, while we are talking about data types...

I am creating a "gateway" function for a fortran routine originally written by a third party.
Of course, I would like not to touch the code given me.

The problem is: the routine I will call from the gateway defines "real" and "integer" variables.
What do integer and real types correspond to in terms of integer*1, integer*2, ... and real*4, real*8, ... ?
Is there anyway to force integer types to be interpreted as integer*4?
Likewise, is there anyway to force real types to be interpreted as real*8?

Alternatively, I could just replace all integer and real declarations by integer*4 and real*8 but Fortran is not exactly something I am familiar with so I'd like to keep tinkering with it to a minimum...

Thanks again.

Best,

G.


"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i43q9v$kj8$1(a)fred.mathworks.com>...
> "Guilherme Rocha" <gvrocha(a)gmail.com> wrote in message <i43njn$r3a$1(a)fred.mathworks.com>...
> > I am a bit confused about the documentation in
> > http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/f33356.html#f15433
> >
> > The documentation directs us to "use the mxCreate functions to create the MATLAB arrays for [our] output argument".
> > However, .
> > Since there is not a specific mxCreate function to create an array of integers,
> > what should I do to allocate space for a matrix of integers?
>
> By "integers" I assume you mean integer type variables, not double variables with integral values. You can use mxCreateNumericMatrix or mxCreateNumericArray for this.
>
> > Should I just create the array of integers using mxCreateDoubleArray and let mxCopyInteger4ToPtr deal with type conversions?
>
> No. That will give the wrong class type to the variable.
>
> > In other words: does the "double" in mxCreateDoubleArray refer to the type of variable (integer, float, double) or does it mean that the array is 2-dimensional (aka matrix).
>
> No. The "double" refers to the class of the MATLAB variable ... it has nothing to do with the size or dimensions of the variable.
>
> > Inicidental question: if the return value is a scalar, do I need to create a 1x1 array using mxCreateDoubleArray or is there a better way to deal with that?
>
> If you want to return a double scalar, use mxCreateDoubleScalar. If you want to return any other type of scalar (e.g., int32), then you will have to use mxCreateNumericMatrix along with mxGetData and appropriate mxCopy___ routines to create the mxArray scalar.
>
>
> James Tursa
From: Steven_Lord on


"Guilherme Rocha" <gvrocha(a)gmail.com> wrote in message
news:i43s44$hdh$1(a)fred.mathworks.com...
> Thanks, that explains a lot.
> And, while we are talking about data types...
> I am creating a "gateway" function for a fortran routine originally
> written by a third party.
> Of course, I would like not to touch the code given me.
> The problem is: the routine I will call from the gateway defines "real"
> and "integer" variables.
> What do integer and real types correspond to in terms of integer*1,
> integer*2, ... and real*4, real*8, ... ?

Look at the table given on the reference page for mxCreateNumericMatrix:

http://www.mathworks.com/access/helpdesk/help/techdoc/apiref/mxcreatenumericmatrix.html

> Is there anyway to force integer types to be interpreted as integer*4?

From which side, MATLAB or Fortran? Variables of class int32 in MATLAB
correspond to Fortran's INTEGER*4 type.

> Likewise, is there anyway to force real types to be interpreted as
> real*8?

Variables of class double in MATLAB correspond to REAL*8 in Fortran.

*snip*

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

From: James Tursa on
"Guilherme Rocha" <gvrocha(a)gmail.com> wrote in message <i43s44$hdh$1(a)fred.mathworks.com>...
>
> The problem is: the routine I will call from the gateway defines "real" and "integer" variables.
> What do integer and real types correspond to in terms of integer*1, integer*2, ... and real*4, real*8, ... ?
> Is there anyway to force integer types to be interpreted as integer*4?
> Likewise, is there anyway to force real types to be interpreted as real*8?
>
> Alternatively, I could just replace all integer and real declarations by integer*4 and real*8 but Fortran is not exactly something I am familiar with so I'd like to keep tinkering with it to a minimum...

The type "integer" in your Fortran is almost certainly an integer*4 type. This will probably be the case even if you are on a 64-bit system. So there will likely be no change required here.

For the real type, it is almost certainly real*4. You can sometimes force these to be real*8 with a compiler switch (look at the compiler doc). That would be the preferred approach. If you manually change all the real to real*8 in the source code, you need to beware of a potential pitfall since any real constants in the program will remain real unless you explicitly convert them in your code. This can bite you, particularly if there are any such constants as part of an argument list to a function or subroutine. Unfortunately, Fortran will not do automatic type promotion of arguments like C and C++ do. If you are not familiar with Fortran then I would suggest avoiding this route if you can. How long is your Fortran code?


James Tursa
 |  Next  |  Last
Pages: 1 2
Prev: solving 2 functions
Next: algebraic loop