Thread Subject: call by reference

Subject: call by reference

From: Jeroen

Date: 12 Apr, 2008 09:33:27

Message: 1 of 7

Hi all,

For some GPS related calculations, I have a bunch of vectors and variables which are needed in a lot of functions.

The thing is that all those date belong togehter in some way. When I would program in Jave, I would make a class from it.

So, what I did is making a new matlab data type which bundles all this information. I added some functions such that it acts just as a struct: I can acces the columns and the elements. The nice advantage is that I could modify the display function, such that I get a nice overview of the data when omitting the ;

But, when I pass this data type as argument to a function, do some manipulations on it, and want the function to return it, it slows down my program due to call by value.

Is there a way to work with pointers and adresses like is possible in C? Or is there perhaps another solution for my problem?

All hints are really appreciated!

Many thanks,
J.

Subject: call by reference

From: Bruno Luong

Date: 12 Apr, 2008 10:25:03

Message: 2 of 7

Jeroen <jverhoeve@hotmail.com> wrote in message
<8902033.1207992837734.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi all,
>
> For some GPS related calculations, I have a bunch of
vectors and variables which are needed in a lot of functions.
>
> The thing is that all those date belong togehter in some
way. When I would program in Jave, I would make a class from it.
>
> So, what I did is making a new matlab data type which
bundles all this information. I added some functions such
that it acts just as a struct: I can acces the columns and
the elements. The nice advantage is that I could modify the
display function, such that I get a nice overview of the
data when omitting the ;
>
> But, when I pass this data type as argument to a function,
do some manipulations on it, and want the function to return
it, it slows down my program due to call by value.
>
> Is there a way to work with pointers and adresses like is
possible in C? Or is there perhaps another solution for my
problem?
>
> All hints are really appreciated!
>
> Many thanks,
> J.

"Inplace" package in FEX might be what you are looking for:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=11290&objectType=file

Warning: reference argument passing is not supported by
Mathworks, and they do anything to discourage users to use them.

Bruno

Subject: call by reference

From: Jeroen

Date: 12 Apr, 2008 10:38:24

Message: 3 of 7

Thanks Bruno,

This solves one of the two problems:
- call by reference

The other one remains:
- how to keep data bundled (I do it by defining my own data type) together and pass it in a time economic way as an argument.

Note: My data type consists of 10 vectors of 30 elements, together with some variables.


The package you suggest can only work with vectors and matrices, while I want to pass my own defined data types by reference.

Subject: call by reference

From: Bruno Luong

Date: 12 Apr, 2008 11:53:01

Message: 4 of 7

Jeroen <jverhoeve@hotmail.com> wrote in message
<8672860.1207996734521.JavaMail.jakarta@nitrogen.mathforum.org>...

>
>
> The package you suggest can only work with vectors and
matrices, while I want to pass my own defined data types by
reference.

MATLAB engine has a smart mechanism that avoids to duplicate
data when it is not necessary, even when passing parameter
by value. The only (?) data type that requires a large
amount of (contiguous) memory and needed to be duplicate
entirely when a element is changed is array. That's why the
inplace FEX program works on array.

You might want to pass your structure object as value, and
use inplace for subfields that are big arrays.

Otherwise you might have to program your own mex function
with (structure) parameters passing as reference.

Please see the recent thread for various opinions and
informations:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/163743

Bruno

Subject: call by reference

From: Stuart McGarrity

Date: 12 Apr, 2008 19:10:06

Message: 5 of 7

Jeroen <jverhoeve@hotmail.com> wrote in message
<8902033.1207992837734.JavaMail.jakarta@nitrogen.mathforum.
org>...
> Hi all,
>
> For some GPS related calculations, I have a bunch of
vectors and variables which are needed in a lot of
functions.
>
> The thing is that all those date belong togehter in some
way. When I would program in Jave, I would make a class
from it.
>
> So, what I did is making a new matlab data type which
bundles all this information. I added some functions such
that it acts just as a struct: I can acces the columns and
the elements. The nice advantage is that I could modify
the display function, such that I get a nice overview of
the data when omitting the ;
>
> But, when I pass this data type as argument to a
function, do some manipulations on it, and want the
function to return it, it slows down my program due to
call by value.
>
> Is there a way to work with pointers and adresses like
is possible in C? Or is there perhaps another solution for
my problem?
>
> All hints are really appreciated!
>
> Many thanks,
> J.
I recommend you take a look at:
1) Specifying reference behaviour with handle classes in
R2008a:
http://www.mathworks.com/access/helpdesk/help/techdoc/matla
b_oop/brfylwk-1.html

2)How MATLAB's copy-on-write behaviour for value objects
works:
http://blogs.mathworks.com/loren/2006/05/10/memory-
management-for-functions-and-variables/

3)In-place calling behaviour:
http://blogs.mathworks.com/loren/2007/03/22/in-place-
operations-on-data/

Stuart

Subject: call by reference

From: Peter Boettcher

Date: 14 Apr, 2008 13:47:06

Message: 6 of 7

Jeroen <jverhoeve@hotmail.com> writes:

> Hi all,
>
> For some GPS related calculations, I have a bunch of vectors and
> variables which are needed in a lot of functions.
>
> The thing is that all those date belong togehter in some way. When I
> would program in Jave, I would make a class from it.
>
> So, what I did is making a new matlab data type which bundles all this
> information. I added some functions such that it acts just as a
> struct: I can acces the columns and the elements. The nice advantage
> is that I could modify the display function, such that I get a nice
> overview of the data when omitting the ;
>
> But, when I pass this data type as argument to a function, do some
> manipulations on it, and want the function to return it, it slows down
> my program due to call by value.
>
> Is there a way to work with pointers and adresses like is possible in
> C? Or is there perhaps another solution for my problem?

The only reason I would pursue in-place computation is if you have a
very large dataset, in the hundreds of megabytes. Otherwise, I would
guess that a simple struct array passed in and out of your functions
will do fine.

MATLAB already makes use of call-by-reference, internally, when a
variable (or part of a cell or struct array) is not modified. How have
you determined that call-by-value is the particular cause of your
performance problem?

-Peter

Subject: call by reference

From: Jeroen

Date: 14 Apr, 2008 18:07:33

Message: 7 of 7

Hi,
indeed, it is aldready done by reference

What I am doing is:
function list = actionOnList(list)

Since the input argument is the same as the output argument, matlab handles it by reference. This is what I figured out on the internet.

I am loosing time in another place in the code, which uses extensively if/else statements. This is in an iterative loop, so I set the constraints to quit the loop less strict.

This solved my performance problem.

Thanks.

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
r2008a handle c... Stuart McGarrity 12 Apr, 2008 15:15:09
rssFeed for this Thread

Contact us at files@mathworks.com