MATLAB Calling matrices from workspace to For loop in MATLAB

AI Thread Summary
The discussion centers on a user needing to extract the first element from multiple matrices (D1 to D36) in their workspace to create a new vector. They sought assistance in using a For loop for this task. A solution was provided using a For loop in MATLAB, which involves dynamically constructing the variable names with the `sprintf` function and executing them with `eval`. The user confirmed that the solution worked successfully.
omri3012
Messages
60
Reaction score
0
Hallo,

I have matrices on my workpace and i need to take the first element from each one
and produce new vector. In other words, i have the matrices D1,D2,D3...D36 on my workspace and by using a For loop i want to generate a new vector:
d=[D1(1,1) D2(1,1)...D3(1,1)].
does anyone know how to that in For loop?

Thanks,
Omri
 
Physics news on Phys.org
How about this:
Code:
for i = 1:36
    s = sprintf('d(i) = D%g(1,1);',i);
    eval(s);
end
 
Thanks,
It's working now, i really appreciate it!

Omri
 
Last edited:

Similar threads

Replies
32
Views
4K
Replies
8
Views
4K
Replies
4
Views
2K
Replies
1
Views
3K
Replies
2
Views
2K
Replies
2
Views
2K
Back
Top