Thread Subject: Input variable from workspace within a function

Subject: Input variable from workspace within a function

From: First Last

Date: 20 Sep, 2007 18:02:25

Message: 1 of 3

I have a variable, var2, in my desktop workspace, which I
can manipulate, declare, and save just fine.

However, I have a function that utilizes the 'input' command
that asks, for instance:

function data = myfn
     variable1 = input('please input variable1: ');
     if isempty(variable1)
         do some task here
     end
     
     data = somecalc(variable1)
end

However I want to assign 'var2' to 'variable1' within the
function call. Without making it an input to the function (I
sort of just answered my question), is there a way of
addressing the variable workspace outside of the function's
workspace?

Thanks ...

Subject: Input variable from workspace within a function

From: David

Date: 20 Sep, 2007 18:33:47

Message: 2 of 3

"First Last" <nospam@nospamplease.com> wrote in message
<fcucjg$k1q$1@fred.mathworks.com>...
> I have a variable, var2, in my desktop workspace, which I
> can manipulate, declare, and save just fine.
>
> However, I have a function that utilizes the 'input'
command
> that asks, for instance:
>
> function data = myfn
> variable1 = input('please input variable1: ');
> if isempty(variable1)
> do some task here
> end
>
> data = somecalc(variable1)
> end
>
> However I want to assign 'var2' to 'variable1' within the
> function call. Without making it an input to the
function (I
> sort of just answered my question), is there a way of
> addressing the variable workspace outside of the
function's
> workspace?
>
> Thanks ...

your 'desktop workspace' is commonly called the 'base'
workspace. each function (not script) gets its own
workspace. functions can access the base workspace using
the evalin and assignin commands, take a look at them in
the help for syntax.

Subject: Input variable from workspace within a function

From: Adam

Date: 20 Sep, 2007 19:16:08

Message: 3 of 3

"David " <dave@bigcompany.com> wrote in message
<fcueeb$jnj$1@fred.mathworks.com>...
> "First Last" <nospam@nospamplease.com> wrote in message
> <fcucjg$k1q$1@fred.mathworks.com>...
> > I have a variable, var2, in my desktop workspace, which I
> > can manipulate, declare, and save just fine.
> >
> > However, I have a function that utilizes the 'input'
> command
> > that asks, for instance:
> >
> > function data = myfn
> > variable1 = input('please input variable1: ');
> > if isempty(variable1)
> > do some task here
> > end
> >
> > data = somecalc(variable1)
> > end
> >
> > However I want to assign 'var2' to 'variable1' within the
> > function call. Without making it an input to the
> function (I
> > sort of just answered my question), is there a way of
> > addressing the variable workspace outside of the
> function's
> > workspace?
> >
> > Thanks ...
>
> your 'desktop workspace' is commonly called the 'base'
> workspace. each function (not script) gets its own
> workspace. functions can access the base workspace using
> the evalin and assignin commands, take a look at them in
> the help for syntax.
>

^^ shudder

You "could" do it that what, but this is what's known as a
bad programming practice. (not a shot at dave, I just don't
want to encourage bad behavior)

use a script file:
  variable1 = input('please input variable1: ');
  
  workspaceData = myfcn(variable1, var2);

This calls your function file
  function data = myfn(variable1, var2)
      if isempty(variable1)
          do some task here
      end
      
      data = somecalc(variable1)
  end

~Adam

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
assignin David 20 Sep, 2007 14:35:23
evalin David 20 Sep, 2007 14:35:23
functions David 20 Sep, 2007 14:35:23
workspace David 20 Sep, 2007 14:35:23
workspace First Last 20 Sep, 2007 14:05:04
input First Last 20 Sep, 2007 14:05:04
functions First Last 20 Sep, 2007 14:05:04
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