Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Newbie question: modular code

Subject: Newbie question: modular code

From: Bryan Benson

Date: 22 Sep, 2010 01:04:08

Message: 1 of 7

Hi,

I'm sure this is a very basic question, but I couldn't find a satisfactory answer online.

I've got a bit of code that runs multiple times, and it runs under multiple conditions. It handles some diverse data (20 separate variable names) differently, depending on the condition. There are another ~20 variables it needs to run properly, which are defined within the function (the people I work with really don't want to handle more than one file)

For the sake of making my code easier to follow (and because to me it is stupid to copy code over), I want to break this (rather long) script into functions, and allow one script to analyze all conditions (instead of having one per condition), just using the condition to guide what operations to do.

The problem I'm having is that passing all of these variables individually to each subfunction is making this approach very tedious. The whole reason I want to do it is to make everything cleaner and easier to follow, and it's having the exact opposite effect.

Is there any way to set a function to have the same scope as the function that calls it, or to operate on the base workspace?

Thank you very much in advance!

Subject: Newbie question: modular code

From: dpb

Date: 22 Sep, 2010 01:04:57

Message: 2 of 7

Bryan Benson wrote:
> Hi,
>
> I'm sure this is a very basic question, but I couldn't find a
> satisfactory answer online.
>
> I've got a bit of code that runs multiple times, and it runs under
> multiple conditions. It handles some diverse data (20 separate variable
> names) differently, depending on the condition. There are another ~20
> variables it needs to run properly, which are defined within the
> function (the people I work with really don't want to handle more than
> one file)
>
> For the sake of making my code easier to follow (and because to me it is
> stupid to copy code over), I want to break this (rather long) script
> into functions, and allow one script to analyze all conditions (instead
> of having one per condition), just using the condition to guide what
> operations to do.
>
> The problem I'm having is that passing all of these variables
> individually to each subfunction is making this approach very tedious.
> The whole reason I want to do it is to make everything cleaner and
> easier to follow, and it's having the exact opposite effect.
>
> Is there any way to set a function to have the same scope as the
> function that calls it, or to operate on the base workspace?
> Thank you very much in advance!

There are GLOBAL variables, but one hesitates to recommend them as a
route to factoring code... :)

Perhaps a little more specificity of what your structure looks like and
the data/being passed could help picturing the actual situation more
clearly.

--

Subject: Newbie question: modular code

From: Bryan Benson

Date: 22 Sep, 2010 02:02:06

Message: 3 of 7

I actually might be able to do it, if I separate it into lots (about 20) of smaller functions.

Is there a performance decrement from making so many function calls?

Thanks again!

dpb <none@non.net> wrote in message <i7bkuq$ubv$1@news.eternal-september.org>...
> Bryan Benson wrote:
> > Hi,
> >
> > I'm sure this is a very basic question, but I couldn't find a
> > satisfactory answer online.
> >
> > I've got a bit of code that runs multiple times, and it runs under
> > multiple conditions. It handles some diverse data (20 separate variable
> > names) differently, depending on the condition. There are another ~20
> > variables it needs to run properly, which are defined within the
> > function (the people I work with really don't want to handle more than
> > one file)
> >
> > For the sake of making my code easier to follow (and because to me it is
> > stupid to copy code over), I want to break this (rather long) script
> > into functions, and allow one script to analyze all conditions (instead
> > of having one per condition), just using the condition to guide what
> > operations to do.
> >
> > The problem I'm having is that passing all of these variables
> > individually to each subfunction is making this approach very tedious.
> > The whole reason I want to do it is to make everything cleaner and
> > easier to follow, and it's having the exact opposite effect.
> >
> > Is there any way to set a function to have the same scope as the
> > function that calls it, or to operate on the base workspace?
> > Thank you very much in advance!
>
> There are GLOBAL variables, but one hesitates to recommend them as a
> route to factoring code... :)
>
> Perhaps a little more specificity of what your structure looks like and
> the data/being passed could help picturing the actual situation more
> clearly.
>
> --

Subject: Newbie question: modular code

From: Ross W

Date: 22 Sep, 2010 03:15:16

Message: 4 of 7

"Bryan Benson" <not@an.address> wrote in message <i7bkm8$28e$1@fred.mathworks.com>...
> Hi,
>
> I'm sure this is a very basic question, but I couldn't find a satisfactory answer online.
>
> I've got a bit of code that runs multiple times, and it runs under multiple conditions. It handles some diverse data (20 separate variable names) differently, depending on the condition. There are another ~20 variables it needs to run properly, which are defined within the function (the people I work with really don't want to handle more than one file)
>
> For the sake of making my code easier to follow (and because to me it is stupid to copy code over), I want to break this (rather long) script into functions, and allow one script to analyze all conditions (instead of having one per condition), just using the condition to guide what operations to do.
>
> The problem I'm having is that passing all of these variables individually to each subfunction is making this approach very tedious. The whole reason I want to do it is to make everything cleaner and easier to follow, and it's having the exact opposite effect.
>
> Is there any way to set a function to have the same scope as the function that calls it, or to operate on the base workspace?
>
> Thank you very much in advance!

Two thoughts:
A nested function has access to the workspaces of all the functions in which it is nested.
Structures are sometimes a convenient means of passing around a collection of related variables using a compact argument list.

Ross

Subject: Newbie question: modular code

From: Bryan Benson

Date: 22 Sep, 2010 03:43:21

Message: 5 of 7

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i7bsc4$5nf$1@fred.mathworks.com>...
> "Bryan Benson" <not@an.address> wrote in message <i7bkm8$28e$1@fred.mathworks.com>...
> > Hi,
> >
> > I'm sure this is a very basic question, but I couldn't find a satisfactory answer online.
> >
> > I've got a bit of code that runs multiple times, and it runs under multiple conditions. It handles some diverse data (20 separate variable names) differently, depending on the condition. There are another ~20 variables it needs to run properly, which are defined within the function (the people I work with really don't want to handle more than one file)
> >
> > For the sake of making my code easier to follow (and because to me it is stupid to copy code over), I want to break this (rather long) script into functions, and allow one script to analyze all conditions (instead of having one per condition), just using the condition to guide what operations to do.
> >
> > The problem I'm having is that passing all of these variables individually to each subfunction is making this approach very tedious. The whole reason I want to do it is to make everything cleaner and easier to follow, and it's having the exact opposite effect.
> >
> > Is there any way to set a function to have the same scope as the function that calls it, or to operate on the base workspace?
> >
> > Thank you very much in advance!
>
> Two thoughts:
> A nested function has access to the workspaces of all the functions in which it is nested.
> Structures are sometimes a convenient means of passing around a collection of related variables using a compact argument list.
>
> Ross

Thanks Ross. Using structures is what I'm doing now.
I still don't really know whether multiple function definitions will slow things down significantly.

As an aside, I'm running into the following issue:
Let's say I have a cell array that looks like this (from struct2cell):
{1x12 cell}
{1x12 cell}
{1x12 cell}

Is there any way to concatenate that into a 3x12 cell without a for loop? I'm sorry if this is obvious. I'm pretty tired. Thanks!

Subject: Newbie question: modular code

From: Bryan Benson

Date: 22 Sep, 2010 04:02:19

Message: 6 of 7

"Bryan Benson" <not@an.address> wrote in message <i7bu0p$jkq$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i7bsc4$5nf$1@fred.mathworks.com>...
> > "Bryan Benson" <not@an.address> wrote in message <i7bkm8$28e$1@fred.mathworks.com>...
> > > Hi,
> > >
> > > I'm sure this is a very basic question, but I couldn't find a satisfactory answer online.
> > >
> > > I've got a bit of code that runs multiple times, and it runs under multiple conditions. It handles some diverse data (20 separate variable names) differently, depending on the condition. There are another ~20 variables it needs to run properly, which are defined within the function (the people I work with really don't want to handle more than one file)
> > >
> > > For the sake of making my code easier to follow (and because to me it is stupid to copy code over), I want to break this (rather long) script into functions, and allow one script to analyze all conditions (instead of having one per condition), just using the condition to guide what operations to do.
> > >
> > > The problem I'm having is that passing all of these variables individually to each subfunction is making this approach very tedious. The whole reason I want to do it is to make everything cleaner and easier to follow, and it's having the exact opposite effect.
> > >
> > > Is there any way to set a function to have the same scope as the function that calls it, or to operate on the base workspace?
> > >
> > > Thank you very much in advance!
> >
> > Two thoughts:
> > A nested function has access to the workspaces of all the functions in which it is nested.
> > Structures are sometimes a convenient means of passing around a collection of related variables using a compact argument list.
> >
> > Ross
>
> Thanks Ross. Using structures is what I'm doing now.
> I still don't really know whether multiple function definitions will slow things down significantly.
>
> As an aside, I'm running into the following issue:
> Let's say I have a cell array that looks like this (from struct2cell):
> {1x12 cell}
> {1x12 cell}
> {1x12 cell}
>
> Is there any way to concatenate that into a 3x12 cell without a for loop? I'm sorry if this is obvious. I'm pretty tired. Thanks!

This is what I came up with (with general variable names)
celldata=struct2cell(orderfields(structuredata,perm));
clear structuredata
length=length(celldata);
output=reshape([celldata{1:length}],width,number)';
clear celldata
csvwrite('outputfilename.csv',output)

Subject: Newbie question: modular code

From: dpb

Date: 22 Sep, 2010 12:52:49

Message: 7 of 7

Bryan Benson wrote:
> I actually might be able to do it, if I separate it into lots (about 20)
> of smaller functions.
>
> Is there a performance decrement from making so many function calls?
...

What do you think is "so many"?

Virtually all of Matlab is a function call, firstly... :)

I would say in general worry about structure and clarity in coding first
and foremost.

Secondly, I'd not worry about function calls unless your nesting a very
narrow and deep call stack inside a very, very long iterative process or
something similar. ML is pretty good about not causing unnecessary
copies, etc., ...

--

Tags for this Thread

No tags are associated with this thread.

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.

Contact us