help-octave
[Top][All Lists]
Advanced

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

Re: Possible bug? cell array indexing problem


From: Jordi Gutiérrez Hermoso
Subject: Re: Possible bug? cell array indexing problem
Date: Tue, 18 Dec 2012 16:18:00 -0500

On 18 December 2012 16:06, mass168 <address@hidden> wrote:
> Thanks for the help guys, eval() solved my problem. The following code now
> works exactly like I needed it to.
>
> y={[1,2,3];[5,8,9];[35:2:46]}
> n1=3;
> n2=2;
> n3=6;
>
> for n103=1:3
> a=num2str(n103)
> b=strcat('n',a)
> y{3,1}(1,eval(b))
> endfor

This is a very weird and brittle way of writing code. Instead of
creating many variables named n1, n2, n3... you should create a single
array called n and refer to n(1), n(2), n(3).

Your code above should be:

    y={[1,2,3];[5,8,9];[35:2:46]};

    n(1)=3;
    n(2)=2;
    n(3)=6;

    for k = n
      y{3,1}(1,k)
    endfor

You don't need to be playing with num2str, strcat, and eval just to
get an array.

- Jordi G. H.


reply via email to

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