Matlab Problem with loops and genvarname

In summary, the conversation discusses a problem with generating code for image processing in MATLAB. The individual is attempting to create a loop that will generate three separate variables, C, C1, and C2, but is encountering errors. They are seeking help and a solution is provided using the eval function.
  • #1
Zayri7
1
0
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 I am 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'])

end
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 I am having problem explaining.

I want to do something like this:

for n=1:3

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

end
But it tells me:

Cell contents reference from a non-cell array object.
I really don't know what to do. Can someone help me?
 
Physics news on Phys.org
  • #2
Ok i don't 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 isn't 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;
clc;
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))
end

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

1. What is the purpose of using loops in Matlab?

Loops in Matlab are used to execute a set of statements repeatedly until a certain condition is met. This helps to automate repetitive tasks and make the code more efficient.

2. What is the difference between for and while loops in Matlab?

The for loop in Matlab executes a set of statements a predetermined number of times, while the while loop executes the statements until a specified condition is no longer true. For loops are typically used when the number of iterations is known beforehand, while while loops are used when the number of iterations is not known.

3. How can I generate variable names using genvarname in Matlab?

The genvarname function in Matlab allows you to generate variable names based on a base name and a number. This is useful when you need to create a large number of variables in a loop, where each variable needs a unique name. The function automatically adds a number at the end of the base name to create a unique variable name.

4. Can I use genvarname to generate variable names with a specific prefix?

Yes, you can use the 'prefix' parameter in the genvarname function to specify a prefix for the generated variable names. This is useful when you want all the variables to have a common prefix, making it easier to identify them in your code.

5. Are there any limitations to using genvarname in Matlab?

Yes, the genvarname function in Matlab has a few limitations. It cannot generate variable names that are longer than 63 characters, and it cannot generate names that are already in use. It also cannot generate names starting with a number or a reserved keyword in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
556
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
117
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Back
Top