Thread Subject: Using functions and structures sensibly

Subject: Using functions and structures sensibly

From: Pete

Date: 8 May, 2008 13:10:20

Message: 1 of 6

Hi,

I have some code that works, but I want to check whether I'm doing
something horribly inefficient or unwise based upon how MATLAB deals with
function arguments.

Basically, I have a (reasonably) large structure, which has been preallocated
with all the correct fields.
I want to pass it to some functions (it isn't certain in advance which functions)
that will modify some fields of the structure.

A dramatically shortened version of the code might be:

% preallocate structure
S = struct( [field names and initial values] );
% apply functions
for ii = 1:numel(fh)
S = feval(fh{ii}, S);
end

where fh is a cell array containing the names of certain functions stored in M
files which begin something like,

function myStruct = myFunction(myStruct)

I read something that suggested modifying input arguments in MATLAB
functions is bad most of the time. I also wasn't sure about using the same
variable name for both the input and the output, but in my mind this seems
sensible for what I want to do.
Is it sensible, or will it result in unnecessary overhead? Would others here do
things the same way or differently?

Currently my code seems to work well and fast enough, but if it is bad
practice and there is a better way then I'd like to learn it. I'm using 2007b at
the moment, and the code is part of a gui created using GUIDE.

Thanks,

Pete

Subject: Using functions and structures sensibly

From: Peter Boettcher

Date: 8 May, 2008 15:07:07

Message: 2 of 6

"Pete " <pete.dot.bankhead@btinternet.dot.com> writes:

> Hi,
>
> I have some code that works, but I want to check whether I'm doing
> something horribly inefficient or unwise based upon how MATLAB deals with
> function arguments.
>
> Basically, I have a (reasonably) large structure, which has been preallocated
> with all the correct fields.
> I want to pass it to some functions (it isn't certain in advance which functions)
> that will modify some fields of the structure.
>
> A dramatically shortened version of the code might be:
>
> % preallocate structure
> S = struct( [field names and initial values] );
> % apply functions
> for ii = 1:numel(fh)
> S = feval(fh{ii}, S);
> end
>
> where fh is a cell array containing the names of certain functions stored in M
> files which begin something like,
>
> function myStruct = myFunction(myStruct)
>
> I read something that suggested modifying input arguments in MATLAB
> functions is bad most of the time. I also wasn't sure about using the same
> variable name for both the input and the output, but in my mind this seems
> sensible for what I want to do.
> Is it sensible, or will it result in unnecessary overhead? Would others here do
> things the same way or differently?

Yes, that's sensible. For purposes of the copy-on-write behavior,
fields of structures (and elements of cell arrays) behave the same as
independent variables. That is, if you modify one field of S, only the
struct header (~100-200 bytes) and the modified field are copied.

-Peter

Subject: Using functions and structures sensibly

From: Pete

Date: 8 May, 2008 15:21:04

Message: 3 of 6

Excellent, that's good to know - thanks!

Subject: Using functions and structures sensibly

From: someone

Date: 8 May, 2008 15:30:55

Message: 4 of 6

"Pete " <pete.dot.bankhead@btinternet.dot.com> wrote in
message <fvuu3s$bd$1@fred.mathworks.com>...
> Hi,
>
> I have some code that works, but I want to check whether
I'm doing
> something horribly inefficient or unwise based upon how
MATLAB deals with
> function arguments.
>
> Basically, I have a (reasonably) large structure, which
has been preallocated
> with all the correct fields.
> I want to pass it to some functions (it isn't certain in
advance which functions)
> that will modify some fields of the structure.
>
> A dramatically shortened version of the code might be:
>
> % preallocate structure
> S = struct( [field names and initial values] );
> % apply functions
> for ii = 1:numel(fh)
> S = feval(fh{ii}, S);
> end
>
> where fh is a cell array containing the names of certain
functions stored in M
> files which begin something like,
>
> function myStruct = myFunction(myStruct)
>
> I read something that suggested modifying input arguments
in MATLAB
> functions is bad most of the time.

The only reason why I know modifying inputs "is bad" is
that MATLAB will pass the inputs by value (instead of by
reference). But, one way or another, you are creating
another copy of myStruct anyway.

> I also wasn't sure about using the same
> variable name for both the input and the output, but in
my mind this seems
> sensible for what I want to do.

You could use something like:

function myStruct = myFunction(myInpStruct)
myStruct = myInpStruct;
...

and call it exactly the same way:

myStruct = myFunction(myStruct)

But, I don't know that this is any more efficient. (Might
make it easier to debug MyFunction.)

> Is it sensible, or will it result in unnecessary
overhead? Would others here do
> things the same way or differently?
>
> Currently my code seems to work well and fast enough, but
if it is bad
> practice and there is a better way then I'd like to learn
it. I'm using 2007b at
> the moment, and the code is part of a gui created using
GUIDE.
>
> Thanks,
>
> Pete

Subject: Using functions and structures sensibly

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 8 May, 2008 16:13:14

Message: 5 of 6

In article <fvv6bf$4gd$1@fred.mathworks.com>,
someone <someone@somewhere.net> wrote:

>The only reason why I know modifying inputs "is bad" is
>that MATLAB will pass the inputs by value (instead of by
>reference). But, one way or another, you are creating
>another copy of myStruct anyway.

I -gather- (perhaps incorrectly) from other postings that the
special case of having the same variable on input and output
of a function is handled by modifying the variable in-place
rather than creating a copy of the variable (other than
if the variable happens to share data with another variable.)
--
  "Man's life is but a jest,
   A dream, a shadow, bubble, air, a vapor at the best."
                                         -- George Walter Thornbury

Subject: Using functions and structures sensibly

From: Loren Shure

Date: 8 May, 2008 21:31:01

Message: 6 of 6

In article <fvv8qq$1d8$1@canopus.cc.umanitoba.ca>, roberson@ibd.nrc-
cnrc.gc.ca says...
> In article <fvv6bf$4gd$1@fred.mathworks.com>,
> someone <someone@somewhere.net> wrote:
>
> >The only reason why I know modifying inputs "is bad" is
> >that MATLAB will pass the inputs by value (instead of by
> >reference). But, one way or another, you are creating
> >another copy of myStruct anyway.
>
> I -gather- (perhaps incorrectly) from other postings that the
> special case of having the same variable on input and output
> of a function is handled by modifying the variable in-place
> rather than creating a copy of the variable (other than
> if the variable happens to share data with another variable.)
>

Sometimes... depends on how it's called from the caller (same right and
left hand side name required) AND requires the calculation to be do-able
inplace.

--
Loren
http://blogs.mathworks.com/loren/

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
function arguments Pete 8 May, 2008 09:15:10
efficient progr... Pete 8 May, 2008 09:15:10
structures Pete 8 May, 2008 09:15:09
rssFeed for this Thread

Contact us at files@mathworks.com