Thread Subject: Using workspace variable in m-file

Subject: Using workspace variable in m-file

From: jay kumar

Date: 6 Sep, 2008 06:33:02

Message: 1 of 9

Hi,
I want to use workspace variables in my m-file and i want to do some operation on that variables in m-file. but that variables and their values are available in workspace.

e.g I want to multiply two transfer functions and obtain bode plot for different values of parameters but that two transfer functions are available in workspace

I do not want to write transfer functions again in m-file. Instead of that i want to obtain it from workspace.

Subject: Using workspace variable in m-file

From: Bruno Luong

Date: 6 Sep, 2008 07:47:01

Message: 2 of 9

"jay kumar" <kumar.jay.123@gmail.com> wrote in message <g9t86u$cu5$1@fred.mathworks.com>...
> Hi,
> I want to use workspace variables in my m-file and i want to do some operation on that variables in m-file. but that variables and their values are available in workspace.
>
> e.g I want to multiply two transfer functions and obtain bode plot for different values of parameters but that two transfer functions are available in workspace
>
> I do not want to write transfer functions again in m-file. Instead of that i want to obtain it from workspace.

What's wrong with passing the value of those variables (could be function handle of the transfer functions) into your m file???

The mechanism of input and output for functions is designed so that direct messing between workspace is minimal.

But if you insist, there is EVALIN for that.

Bruno

Subject: Using workspace variable in m-file

From: Husam Aldahiyat

Date: 6 Sep, 2008 07:56:01

Message: 3 of 9

Use global. Solves all life's problems.

Subject: Using workspace variable in m-file

From: John D'Errico

Date: 6 Sep, 2008 08:51:02

Message: 4 of 9

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <g9td2h$se1$1@fred.mathworks.com>...
> Use global. Solves all life's problems.

Yes, and it creates other problems. Globals
are sloppy, lazy programming.

Pass the variables in. If they are functions,
perhaps anonymous functions, or simply
variables, then it is far simpler to pass them
in as arguments to the function. This solves
the OP's problems without creating other
ones.

John

Subject: Using workspace variable in m-file

From: Andrew

Date: 6 Sep, 2008 14:01:46

Message: 5 of 9

On Sep 6, 12:33 am, "jay kumar" <kumar.jay....@gmail.com> wrote:
> Hi,
> I want to use workspace variables in my m-file and i want to do some operation on that variables in m-file. but that variables and their values are available in workspace.
>
> e.g I want to multiply two transfer functions and obtain bode plot for different values of parameters but that two transfer functions are available in workspace
>
> I do not want to write transfer functions again in m-file. Instead of that i want to obtain it from workspace.

If you define the transfer functions in the workspace (from the
command page, I assume), then they are already in the workspace. The
m-file will already see the variable, unless you begin your m-files as
I do with clear all; close all commands. Once the variables are in
the workspace, the m-file will automatically recognize them.

Subject: Using workspace variable in m-file

From: John D'Errico

Date: 6 Sep, 2008 14:42:01

Message: 6 of 9

Andrew <andrewkgentile@gmail.com> wrote in message <33eac32d-6e15-4ff2-895f-f04ec68458d0@d1g2000hsg.googlegroups.com>...
> On Sep 6, 12:33 am, "jay kumar" <kumar.jay....@gmail.com> wrote:
> > Hi,
> > I want to use workspace variables in my m-file and i want to do some operation on that variables in m-file. but that variables and their values are available in workspace.
> >
> > e.g I want to multiply two transfer functions and obtain bode plot for different values of parameters but that two transfer functions are available in workspace
> >
> > I do not want to write transfer functions again in m-file. Instead of that i want to obtain it from workspace.
>
> If you define the transfer functions in the workspace (from the
> command page, I assume), then they are already in the workspace. The
> m-file will already see the variable, unless you begin your m-files as
> I do with clear all; close all commands. Once the variables are in
> the workspace, the m-file will automatically recognize them.

This is only true if the m-file is a script.

Scripts are generally a poor choice, especially
if you begin them with clear commands.

Far better is to learn to use and write functions.
Functions have their own workspace, so you
need not worry about polluting the base
workspace with the odd variables created inside
your functions. This is a far cleaner way to
build your tools. Make small functions that do
a single thing. In some languages these are
called idioms. Build up an idiom library of your
own, formed both from the functions you have
written to solve your own specific type of
problems, and from those useful utilities that
you have downloaded from the file exchange.

You will be surprised how fast these functions
accumulate, as well as how useful they will be.
Before long, other people you work with will
find out about your collection of idioms.

John

Subject: Using workspace variable in m-file

From: Andrew

Date: 7 Sep, 2008 13:53:33

Message: 7 of 9

On Sep 6, 8:42 am, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> Andrew <andrewkgent...@gmail.com> wrote in message <33eac32d-6e15-4ff2-895f-f04ec6845...@d1g2000hsg.googlegroups.com>...
> > On Sep 6, 12:33 am, "jay kumar" <kumar.jay....@gmail.com> wrote:
> > > Hi,
> > > I want to use workspace variables in my m-file and i want to do some operation on that variables in m-file. but that variables and their values are available in workspace.
>
> > > e.g I want to multiply two transfer functions and obtain bode plot for different values of parameters but that two transfer functions are available in workspace
>
> > > I do not want to write transfer functions again in m-file. Instead of that i want to obtain it from workspace.
>
> > If you define the transfer functions in the workspace (from the
> > command page, I assume), then they are already in the workspace. The
> > m-file will already see the variable, unless you begin your m-files as
> > I do with clear all; close all commands. Once the variables are in
> > the workspace, the m-file will automatically recognize them.
>
> This is only true if the m-file is a script.
>
> Scripts are generally a poor choice, especially
> if you begin them with clear commands.
>
> Far better is to learn to use and write functions.
> Functions have their own workspace, so you
> need not worry about polluting the base
> workspace with the odd variables created inside
> your functions. This is a far cleaner way to
> build your tools. Make small functions that do
> a single thing. In some languages these are
> called idioms. Build up an idiom library of your
> own, formed both from the functions you have
> written to solve your own specific type of
> problems, and from those useful utilities that
> you have downloaded from the file exchange.
>
> You will be surprised how fast these functions
> accumulate, as well as how useful they will be.
> Before long, other people you work with will
> find out about your collection of idioms.
>
> John

John,
I have been using m-files (as scripts) and functions for about a
decade, and I have never considered the benefit of writing my scripts
as functions.
Thanks for the tip. I will consider it.

Andy

Subject: Using workspace variable in m-file

From: ImageAnalyst

Date: 7 Sep, 2008 14:12:48

Message: 8 of 9

On Sep 6, 4:51=A0am, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> "Husam Aldahiyat" <numand...@gmail.com> wrote in message <g9td2h$se...@fr=
ed.mathworks.com>...
> > Use global. Solves all life's problems.
>
> Yes, and it creates other problems. Globals
> are sloppy, lazy programming.
>
> Pass the variables in. If they are functions,
> perhaps anonymous functions, or simply
> variables, then it is far simpler to pass them
> in as arguments to the function. This solves
> the OP's problems without creating other
> ones.
>
> John

-------------------------------------------
John:
What if the function is the callback of a button (placed with GUIDE)?
How do you pass variables into that? GUIDE automatically puts in 3
arguments. How can you get in other variables that may be needed
inside that function? I've just been using "global" because I don't
know any other way to get stuff in there. For a simple example, let's
say the button should multiply a vector by 2 and plot in in an axes.
How do you get that vector into the callback function of the button?
Thanks,
ImageAnalyst
-----------------------------------------------------------

Subject: Using workspace variable in m-file

From: Steven Lord

Date: 8 Sep, 2008 02:33:40

Message: 9 of 9


"ImageAnalyst" <imageanalyst@mailinator.com> wrote in message
news:51dfc6c1-996a-4a79-a957-218aca2d1ad9@d77g2000hsb.googlegroups.com...
> On Sep 6, 4:51 am, "John D'Errico" <woodch...@rochester.rr.com> wrote:
> > "Husam Aldahiyat" <numand...@gmail.com> wrote in message
> > <g9td2h$se...@fred.mathworks.com>...
> > > Use global. Solves all life's problems.
> >
> > Yes, and it creates other problems. Globals
> > are sloppy, lazy programming.
> >
> > Pass the variables in. If they are functions,
> > perhaps anonymous functions, or simply
> > variables, then it is far simpler to pass them
> > in as arguments to the function. This solves
> > the OP's problems without creating other
> > ones.
> >
> > John
>
> -------------------------------------------
> John:
> What if the function is the callback of a button (placed with GUIDE)?
> How do you pass variables into that? GUIDE automatically puts in 3
> arguments. How can you get in other variables that may be needed
> inside that function? I've just been using "global" because I don't
> know any other way to get stuff in there. For a simple example, let's
> say the button should multiply a vector by 2 and plot in in an axes.
> How do you get that vector into the callback function of the button?

In this case, you can't pass variables into the callback directly, but there
are better ways to handle that situation than using globals -- the usual
suspects are storing values in the UserData or the handles structure and
retrieving them inside the callback function, writing your callback as a
nested functions, using SETAPPDATA/GETAPPDATA, etc.

--
Steve Lord
slord@mathworks.com


Tags for this Thread

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.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com