[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: input string use in a for-statement
From: |
Richardson, Anthony |
Subject: |
RE: input string use in a for-statement |
Date: |
Mon, 20 Aug 2012 17:07:09 +0000 |
> -----Original Message-----
> From: address@hidden [mailto:help-octave-
> address@hidden On Behalf Of Carnë Draug
> Sent: Monday, August 20, 2012 10:20 AM
> To: Nikolai
> Cc: address@hidden
> Subject: Re: input string use in a for-statement
>
> On 20 August 2012 15:48, Nikolai <address@hidden> wrote:
> > Hello,
> >
> > I have this piece of code:
> >
> > Var1 = xxx;
> > Var2 = xxx;
> > ...
> > ...
> > EingParameter = input ("Veränderbaren Parameter: ","s") EingPStart =
> > input ("Parameter Startwert eingeben ") EingPEnd = input ("Parameter
> > Endwert eingeben ") EingPSteps = input ("Paramter Anzahl eingeben ")
> >
> > for EingParameter = linspace(EingPStart,EingPEnd,EingPSteps)
> >
> > What I want is, for the user to enter the name of a variable in the
> > first input prompt so that the for-expression uses that corresponding
> > variable. I know that this code won't work the way i want it to, but
> > ideally it would look like this.
> >
> > Does anyone know what I would have to do, to get this working the way
> > I want to?
> >
> > Thanks in advance for any help!
>
> I don't understand. It works for me:
>
> octave:8> var1 = 45;
> octave:9> var2 = 78;
> octave:10> var3 = 99;
> octave:11> EingPStart = input ("Parameter Startwert eingeben ") Parameter
> Startwert eingeben var3 EingPStart = 99
>
> Carnë
I believe the OP wants to be prompted for the variable name. You can use the
eval function to do what you want. Here is an example script:
varname = input("Enter var name: ", "s");
stval = input("Enter starting value: ");
edval = input("Enter ending value: ");
snval = input("Enter number of values: ");
eval(sprintf("%s = linspace(%d, %d, %d)",varname, stval, edval, snval));
Add another ";" before the second " to suppress display of the variable value
during evaluation.
Tony Richardson