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:
global variables in function

Subject: global variables in function

From: Jose

Date: 28 Jul, 2010 14:27:08

Message: 1 of 11

I want to hear about the expert in matlab if the use global variables
in matlab function or not...i.e...

you think is a good idea to call a function from the main program passing the

varaibles in the function as prueba(x,y,z), or is better to write down:

        function prueba

         global x y z


What do you think?

My question is if I have 20 variables to work with in the function, i need to pass

all of them as function(a1,a2,.........a20), or is better use it like global variables.

Thanks in advance, I would like to know the opinion of the expert in Matlab's programmers.

Subject: global variables in function

From: Robert Cumming

Date: 28 Jul, 2010 14:56:04

Message: 2 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <i2pens$ooj$1@fred.mathworks.com>...
> I want to hear about the expert in matlab if the use global variables
> in matlab function or not...i.e...
>
> you think is a good idea to call a function from the main program passing the
>
> varaibles in the function as prueba(x,y,z), or is better to write down:
>
> function prueba
>
> global x y z
>
>
> What do you think?
>
> My question is if I have 20 variables to work with in the function, i need to pass
>
> all of them as function(a1,a2,.........a20), or is better use it like global variables.
>
> Thanks in advance, I would like to know the opinion of the expert in Matlab's programmers.

I dont know if I pass your assessment of as an expert or not... Personally I would do neither, I would create a structure

myStruct.a1 = [yourData1]
myStruct.a2 = [yourData2]
...
myStruct.a20 = [yourData20]

then pass that in as a single variable

 prueba ( myStruct )

Subject: global variables in function

From: Andy

Date: 28 Jul, 2010 15:18:04

Message: 3 of 11

In addition to Robert Cumming's suggestion, depending on your particular problem it may make sense to wrap your 20 arguments into an object and make your function a method for the class. Passing a structure to a function certainly will work, but then you leave it to every function using that structure to validate its contents. It would likely be better to have a separate class. This would allow you to define default values for unsupplied properties, or to validate the properties in a separate method before they need to be used by either a method or a function.

Subject: global variables in function

From: someone

Date: 28 Jul, 2010 15:23:06

Message: 4 of 11

"Robert Cumming" <rcumming@matpi.com> wrote in message <i2pge4$htu$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <i2pens$ooj$1@fred.mathworks.com>...
> > I want to hear about the expert in matlab if the use global variables
> > in matlab function or not...i.e...
> >
> > you think is a good idea to call a function from the main program passing the
> >
> > varaibles in the function as prueba(x,y,z), or is better to write down:
> >
> > function prueba
> >
> > global x y z
> >
> >
> > What do you think?
> >
> > My question is if I have 20 variables to work with in the function, i need to pass
> >
> > all of them as function(a1,a2,.........a20), or is better use it like global variables.
> >
> > Thanks in advance, I would like to know the opinion of the expert in Matlab's programmers.
>
> I dont know if I pass your assessment of as an expert or not... Personally I would do neither, I would create a structure
>
> myStruct.a1 = [yourData1]
> myStruct.a2 = [yourData2]
> ...
> myStruct.a20 = [yourData20]
>
> then pass that in as a single variable
>
> prueba ( myStruct )

I may not be an "expert" either, but I definitely second the structure approach.

Subject: global variables in function

From: Jose

Date: 28 Jul, 2010 15:47:04

Message: 5 of 11

Andy, what you mean it would likely better for the class..please, can you write down an example...follow the Rober's example?



"Andy " <myfakeemailaddress@gmail.com> wrote in message <i2phnc$dhb$1@fred.mathworks.com>...
> In addition to Robert Cumming's suggestion, depending on your particular problem it may make sense to wrap your 20 arguments into an object and make your function a method for the class. Passing a structure to a function certainly will work, but then you leave it to every function using that structure to validate its contents. It would likely be better to have a separate class. This would allow you to define default values for unsupplied properties, or to validate the properties in a separate method before they need to be used by either a method or a function.

Subject: global variables in function

From: Jose

Date: 28 Jul, 2010 15:48:06

Message: 6 of 11

Definitely guys, the idea of passing the variables as global through the function is completelly wrong then.


"Jose " <jose.l.vega@gmail.com> wrote in message <i2pens$ooj$1@fred.mathworks.com>...
> I want to hear about the expert in matlab if the use global variables
> in matlab function or not...i.e...
>
> you think is a good idea to call a function from the main program passing the
>
> varaibles in the function as prueba(x,y,z), or is better to write down:
>
> function prueba
>
> global x y z
>
>
> What do you think?
>
> My question is if I have 20 variables to work with in the function, i need to pass
>
> all of them as function(a1,a2,.........a20), or is better use it like global variables.
>
> Thanks in advance, I would like to know the opinion of the expert in Matlab's programmers.

Subject: global variables in function

From: John D'Errico

Date: 28 Jul, 2010 16:11:06

Message: 7 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <i2pjfm$9uh$1@fred.mathworks.com>...
> Definitely guys, the idea of passing the variables as global through the function is completelly wrong then.
>

I was never pert in my younger days, so ex-pert hardly
seems appropriate.

Passing them as global gets poor style points from
me. I think of globals as a crutch, allowing you to do
things in a way that, while it works, is liable to yield
difficult to debug code. It will get you in the habit of
creating global spaghetti code.

My preference is the struct approach. While I sympathize
with the idea of a custom class for this purpose, that
seems (to me at least) to be a case of using a Mack
truck to carry a pea to Boston - a bit of overkill.

John

Subject: global variables in function

From: Eric

Date: 28 Jul, 2010 16:53:04

Message: 8 of 11

I'm a strong believer in classes, but I'm not sure this would solve the OP's original problem. You could very well define a class and need to pass in 20 arguments into the constructor (or require setting all 20 parameters manually). If many of the parameters could use default values, then some work may be saved.

-Eric

Subject: global variables in function

From: Steven_Lord

Date: 28 Jul, 2010 17:03:28

Message: 9 of 11



"John D'Errico" <woodchips@rochester.rr.com> wrote in message
news:i2pkqp$a9i$1@fred.mathworks.com...
> "Jose " <jose.l.vega@gmail.com> wrote in message
> <i2pjfm$9uh$1@fred.mathworks.com>...
>> Definitely guys, the idea of passing the variables as global through the
>> function is completelly wrong then.
>>
>
> I was never pert in my younger days, so ex-pert hardly
> seems appropriate.
>
> Passing them as global gets poor style points from
> me. I think of globals as a crutch, allowing you to do
> things in a way that, while it works, is liable to yield
> difficult to debug code. It will get you in the habit of
> creating global spaghetti code.

Passing variables normally as input and output arguments from functions is
like sending your bank account number in an envelope through the mail --
sure, someone can tamper with the envelope (using EVALIN, ASSIGNIN, etc.)
but if you see other functions you call using those, they tend to scream
"Danger Will Robinson!"

Sharing variables using global variables is like sending your bank account
number through the mail -- on a postcard. Now it's MUCH easier for people
to interfere with your function's variables without you knowing about it
until your function throws an error or returns the wrong answer (if you're
lucky.)

> My preference is the struct approach. While I sympathize
> with the idea of a custom class for this purpose, that
> seems (to me at least) to be a case of using a Mack
> truck to carry a pea to Boston - a bit of overkill.

It depends on not just what this one function is doing but what other pieces
his or her project has. If the OP is doing this as part of some image
processing application, for instance, and those parameters are different
pieces of data about the camera that captured the image, it might make sense
to create a either a struct or a class to represent the camera. The class
could provide extra parameter validation (say by using property accessor
methods) that would otherwise need to be done whenever the user changed the
struct's fields.

--
Steve Lord
slord@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

Subject: global variables in function

From: Andy

Date: 28 Jul, 2010 17:04:04

Message: 10 of 11

> snip
> While I sympathize
> with the idea of a custom class for this purpose, that
> seems (to me at least) to be a case of using a Mack
> truck to carry a pea to Boston - a bit of overkill.
>
> John

I agree that the class approach is generally overkill. I would say (vaguely) that the more the 20 arguments represent parameters of a single object or experiment and the more that experiment will be repeated (or the more experiments that object might be useful in) then the more it makes sense to use a separate class.

That is, if your data really represents an object, you should make an object out of it.

Subject: global variables in function

From: Matt J

Date: 28 Jul, 2010 17:39:04

Message: 11 of 11

"Jose " <jose.l.vega@gmail.com> wrote in message <i2pens$ooj$1@fred.mathworks.com>...

> What do you think?
>
> My question is if I have 20 variables to work with in the function, i need to pass
>
> all of them as function(a1,a2,.........a20), or is better use it like global variables.
================

In case it's relevant to you, I created the following FEX tool to make it easier to pack/unpack long lists of function arguments to/from structs, as others have been recommending

http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables

Tags for 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