help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: input string use in a for-statement


From: Carnë Draug
Subject: Re: input string use in a for-statement
Date: Mon, 20 Aug 2012 16:58:38 +0100

Please, always make reply to all and include the mailing list.

On 20 August 2012 16:48, Nikolai <address@hidden> wrote:
>
> On 20.08.2012 16:19, Carnë Draug wrote:
>>
>> 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 don't want the value of the respective variable, but rather the use of the
> entered variable in a statement.
> I realize now that the variable doesn't even have to have a value
> beforehand. I just need it in the for-statement:
>
> for EingParameter = linspace(EingPStart,EingPEnd,EingPSteps)
>         [minAJplot,minEJplot] =
> ModellA(Y_1,R_1,Ra,Rb,sigma,theta,n,gammaz,omega,mu,piunten,pioben,xunten,xoben);
>     endfor
>
> I.e. if the user enters "omega" in the prompt, what i want to happen is that
> the for-statement is executed as like this:
>
> for omega = linspace(EingPStart,EingPEnd,EingPSteps)
>         [minAJplot,minEJplot] =
> ModellA(Y_1,R_1,Ra,Rb,sigma,theta,n,gammaz,omega,mu,piunten,pioben,xunten,xoben);
>     endfor


In that case use input (PROMPT, "s") to get a string and then use
eval. For example:

octave:16> a = input ("First var name: ", "s")
First var name: omega
a = omega
octave:17> b = input ("Second var name: ", "s")
Second var name: theta
b = theta
octave:18> omega = 4
omega =  4
octave:19> theta = 10
theta =  10
octave:20> sum ([eval(a) eval(b)])
ans =  14

Carnë


reply via email to

[Prev in Thread] Current Thread [Next in Thread]