How to create automatically the variable name

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create automatically the variable name

Leloup
Hello,
    I should like creating automatically the variable name. I tried :
 >>for K = 1:3,name = ["A",num2str(K)],endfor
name = A1
name = A2
name = A3
 However that fails because I need to associate to each variable A1, A2 to
data. For example A1 = 1, A2 = 2...
I tried:
>> for K = 1:10,["A",num2str(K)] = K,endfor
parse error:   invalid left hand side of assignment
>> for K = 1:3,A{K} = K,endfor
A =
{
  [1,1] =  1
}
A =
{
  [1,1] =  1
  [1,2] =  2
}
A =
{
  [1,1] =  1
  [1,2] =  2
  [1,3] =  3
}
There, class of variable  is cell and I should like getting double

Do you have an idea?

Regards,
Le loup



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Sergei Steshenko


On Sunday, June 24, 2018, 10:51:50 AM GMT+3, Leloup <[hidden email]> wrote:


Hello,
    I should like creating automatically the variable name. I tried :
>>for K = 1:3,name = ["A",num2str(K)],endfor
name = A1
name = A2
name = A3
However that fails because I need to associate to each variable A1, A2 to
data. For example A1 = 1, A2 = 2...
I tried:
>> for K = 1:10,["A",num2str(K)] = K,endfor
parse error:  invalid left hand side of assignment
>> for K = 1:3,A{K} = K,endfor
A =
{
  [1,1] =  1
}
A =
{
  [1,1] =  1
  [1,2] =  2
}
A =
{
  [1,1] =  1
  [1,2] =  2
  [1,3] =  3
}
There, class of variable  is cell and I should like getting double

Do you have an idea?

Regards,
Le loup

--


"Do you have an idea?" - how about using a struct and its keys as variable names ?


Start from 'help struct' within Octave.

--Sergei.









Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Leloup
>> for K = 1:3,struct(["A",num2str(K)],K),endfor
ans =

  scalar structure containing the fields:

    A1 =  1

ans =

  scalar structure containing the fields:

    A2 =  2

ans =

  scalar structure containing the fields:

    A3 =  3

>> A1
error: 'A1' undefined near line 1 column 1 ??????????????????????



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Sergei Steshenko


On Sunday, June 24, 2018, 2:18:49 PM GMT+3, Leloup <[hidden email]> wrote:


>> for K = 1:3,struct(["A",num2str(K)],K),endfor
ans =

  scalar structure containing the fields:

    A1 =  1

ans =

  scalar structure containing the fields:

    A2 =  2

ans =

  scalar structure containing the fields:

    A3 =  3

>> A1
error: 'A1' undefined near line 1 column 1 ??????????????????????




--
Sent from:
http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


You are accessing your struct in a wrong manner.

First of all, you haven't given the struct a name, i.e. you create a struct going nowhere.

Second, you access data in a struct through keys.


--Sergei.





Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Mike Miller-4
In reply to this post by Leloup
On Sun, Jun 24, 2018 at 00:50:49 -0700, Leloup wrote:

> Hello,
>     I should like creating automatically the variable name. I tried :
>  >>for K = 1:3,name = ["A",num2str(K)],endfor
> name = A1
> name = A2
> name = A3
>  However that fails because I need to associate to each variable A1, A2 to
> data. For example A1 = 1, A2 = 2...
> I tried:
> >> for K = 1:10,["A",num2str(K)] = K,endfor
You might find the 'assignin' function useful.

  https://octave.org/doc/interpreter/Evaluation-in-a-Different-Context.html

--
mike



signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Thomas D. Dean-2
In reply to this post by Leloup
On 06/24/18 00:50, Leloup wrote:

> Hello,
>      I should like creating automatically the variable name. I tried :
>   >>for K = 1:3,name = ["A",num2str(K)],endfor
> name = A1
> name = A2
> name = A3
>   However that fails because I need to associate to each variable A1, A2 to
> data. For example A1 = 1, A2 = 2...
> I tried:
>>> for K = 1:10,["A",num2str(K)] = K,endfor
> parse error:   invalid left hand side of assignment

eval(strcat('a',num2str(K),'=',num2str(K+1)))

Tom Dean


Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Andreas Weber-6
In reply to this post by Leloup
Am 24.06.2018 um 09:50 schrieb Leloup:
> Hello,
>     I should like creating automatically the variable name. I tried :
>  >>for K = 1:3,name = ["A",num2str(K)],endfor

You've got many solutions so far but I think you shouldn't do it in the
first place.

You really should work with vectors, matrices or cell arrays instead.

This question comes up on stackoverflow every two weeks but there is no
sane reason to create variables dynamically with a index.

Just my two cents, Andy


Reply | Threaded
Open this post in threaded view
|

Re: How to create automatically the variable name

Leloup
In reply to this post by Thomas D. Dean-2
Bonjour Tom Dean,

>> for K = 1:3,eval(strcat('A',num2str(K),'=',num2str(rand))),endfor
A1 =  0.52979
A2 =  0.68847
A3 =  0.63740

That works!  Thank you for your help Tom  



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html