Matlab Problem with loops and genvarname

by Zayri7
Tags: loop, matlab, matrix, non-cell array
Zayri7 is offline
Aug15-10, 03:19 PM
P: 1
I've been stuck for weeks on a code that I have been generating for image processing. And made an example of what I need. I have this code: (BTW Im new to this matlab world)

A = [2, 5, 6; 3,6,7];

B = [5, 3, 1; 7,3,2];

for i=1:3

v = genvarname('C', who);

eval([v '= A-B'])

The part above ^^^^ gives me this:

C =

-3 2 5
-4 3 5

C1 =

-3 2 5
-4 3 5

C2 =

-3 2 5
-4 3 5

This is the part im having problem explaining.

I want to do something like this:

for n=1:3

F{n} = C{n}(2,1) + B(2,1)

But it tells me:

Cell contents reference from a non-cell array object.
I really dont know what to do. Can someone help me?
Phys.Org News Partner Science news on
Internet co-creator Cerf debunks 'myth' that US runs it
Astronomical forensics uncover planetary disks in Hubble archive
Solar-powered two-seat Sunseeker airplane has progress report
spooky_ush is offline
Mar3-12, 05:59 PM
P: 1
Ok i dont know if you have solved this yet, but looking at the date on the post, you might have but someone may still be looking for something similar.

The reason why this isnt working is already explained in the matlab error statement generated when you run this.

If i understand you correctly you want c{n} to run like c1, c2 and c3.

Just ignoring the fact that this is wrong, even if it were right you would still get an error. There is no C3. You generate C C1 and C2. Moreover C, C1 and C2 can not be structures, they are simple matrices. elements of structures are accessed through c{n}

Now to the part how can you get C C1 and C2 is as follows:

clear all;
A = [2, 5, 6; 3,6,7];
B = [5, 3, 1; 7,3,2];
for i=1:3
eval(sprintf('C%d = A-B',i))
eval(sprintf('F{i} = C%d(2,1)+B(2,1)',i))

dont use %v = genvarname('C', who); eval alone can do the job for you.

Register to reply

Related Discussions
Matlab - I am having problems with iteration (loops) Math & Science Software 19
MATLAB/COMSOL geometry loops? Math & Science Software 1
Matlab: Plotting loops, arrays Math & Science Software 6
for loops in Matlab Engineering, Comp Sci, & Technology Homework 22
Vectorizing loops in MATLAB Math & Science Software 2