cell #3

  • Follow



basically i generate an array elcn=[1 2 3;2 4 3;2 5 4]
the no. of rows are dynamically generated-depends on the 
user.(e=3 in dis e.g)

i want to extract each row and store it in variables 
having different names for e.g for the above prob it wil b 
like dis
E1=[1 2 3];
E2=[2 4 3];
E3=[2 5 4];
i wrote dis loop-
for k=1:e
    eval(['E' num2str(k) ' = elcon(k,:)'])
end
i get this using d 'eval' command as shown above but now 
the prob is i want to use e1,e2,e3 to be called in another 
for loop.
How to call dem?? because if i use E it gives 'undefined 
variable'. and i cant give E1,E2,E3 explicitly as it is 
user defined and could on to be till E50 or so..

please help me.
if possiible tell me another way.


0
Reply pragya.garg (12) 9/23/2007 1:29:56 PM

In article <fd5pok$piu$1@fred.mathworks.com>,
pragya garg <pragya.garg@gmail.com> wrote:

>basically i generate an array elcn=[1 2 3;2 4 3;2 5 4]

>i want to extract each row and store it in variables 
>having different names for e.g for the above prob it wil b 
>like dis
>E1=[1 2 3];
>E2=[2 4 3];
>E3=[2 5 4];

>the prob is i want to use e1,e2,e3 to be called in another 
>for loop.
>How to call dem?? because if i use E it gives 'undefined 
>variable'. and i cant give E1,E2,E3 explicitly as it is 
>user defined and could on to be till E50 or so..

We tell people how to handle this kind of situation nearly every
day, and often two or three times on any given day. Look back at the
last few days of postings, looking for the keyword "eval". If I
recall correctly, within the last week, Steve Lord gave the exact
FAQ (Frequently Asked Questions) reference.


Forgive us for not being more forth-coming with "just the answer": we
are in the position like that of automobile mechanics who, several times
a week, get asked by amateurs "How do I adjust my brakes??" Do we bluntly
say, "We won't tell you how to do that: leave it to the professionals!",
or do we tell how to do it and let people go ahead at serious risk?
-- 
  Prototypes are supertypes of their clones.           -- maplesoft
0
Reply roberson2 (8067) 9/24/2007 12:20:28 AM


"pragya garg" <pragya.garg@gmail.com> wrote in message 
news:fd5pok$piu$1@fred.mathworks.com...
>
>
> basically i generate an array elcn=[1 2 3;2 4 3;2 5 4]
> the no. of rows are dynamically generated-depends on the
> user.(e=3 in dis e.g)
>
> i want to extract each row and store it in variables
> having different names for e.g for the above prob it wil b
> like dis
> E1=[1 2 3];
> E2=[2 4 3];
> E3=[2 5 4];
> i wrote dis loop-
> for k=1:e
>    eval(['E' num2str(k) ' = elcon(k,:)'])
> end
> i get this using d 'eval' command as shown above but now
> the prob is i want to use e1,e2,e3 to be called in another
> for loop.
> How to call dem?? because if i use E it gives 'undefined
> variable'. and i cant give E1,E2,E3 explicitly as it is
> user defined and could on to be till E50 or so..

That's one of the reasons that this programming pattern is strongly 
discouraged.  If you want to dynamically refer elsewhere in your code to the 
new variable you created, you will need to use EVAL there too.

for k=1:e
   eval(['E' num2str(k) ' = elcon(k,:)'])
end

for k = 1:e
  eval(['plot(E' num2str(k) ')'])
end

Note how much more complicated this looks than:

for k = 1:e
  plot(E(k, :));
end

In addition, I almost guarantee you that should you go the EVAL route, 
you'll run into problems with having too many or too few single quotes in 
your EVAL expressions eventually.

-- 
Steve Lord
slord@mathworks.com 


0
Reply slord (13366) 9/24/2007 3:33:27 PM

2 Replies
51 Views

(page loaded in 0.099 seconds)


Reply: