Matlab Problem with loops and genvarname

  • Context: MATLAB 
  • Thread starter Thread starter Zayri7
  • Start date Start date
  • Tags Tags
    Loops Matlab
Click For Summary
SUMMARY

The discussion centers on a MATLAB coding issue involving loops and variable naming. The user attempts to dynamically create variable names using `genvarname` and `eval`, resulting in confusion and errors when accessing these variables. The solution provided involves using `eval` with `sprintf` to generate variable names directly, allowing for correct indexing and access to the resulting matrices. The final code snippet demonstrates how to achieve the desired functionality without the need for `genvarname`.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with matrix operations in MATLAB
  • Knowledge of the `eval` function in MATLAB
  • Basic understanding of loops in programming
NEXT STEPS
  • Learn about MATLAB's `sprintf` function for formatted string generation
  • Explore MATLAB's cell arrays and their indexing methods
  • Study best practices for avoiding the use of `eval` in MATLAB
  • Investigate dynamic variable naming alternatives in MATLAB
USEFUL FOR

MATLAB beginners, image processing developers, and anyone troubleshooting dynamic variable creation in MATLAB scripts.

Zayri7
Messages
1
Reaction score
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
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K