Call elements with different ending in a loop
Show older comments
Hey,
i have different 6x6 arrays in my workspace with names like Ms1, Ms2, Ms3, Msi I want to extract the element in the first row and the first line and create a Vector ix1.
Here is the idea how to do it, but instead of caling Ms1, Ms2 ... i want to call like Msi...
clear A
for i = 1:50
A(i,1) = Ms'i' (1,1);
end
This expression (Ms'i') is wrong, but i dont know how to write it properly.
Would be thankful if anybody could give me the answer to my problem.
Thanks in advance ;)
1 Comment
Do NOT waste your time writing bad code that creates separate variables with numbered variable names: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended."
Just use indexing: simple, fast, efficient, neat, simple, easy to understand, much less buggy, easier to debug, etc, etc, etc. If you are importing those variables then it is trivial to load or import them into one variable in a loop.
https://www.mathworks.com/matlabcentral/answers/143-how-do-i-make-a-series-of-variables-a1-a2-a3-a10
etc, etc, etc.
Accepted Answer
More Answers (1)
Hi Sebastian
If you stack all Msi there's no need to use the for loop to generate vector A:
1.
example with Ms1 Ms2 Ms3
Ms1=randi([-10 10],6)
=
7 2 9 -9 1 -4
-5 -1 -4 -9 -1 1
9 -3 5 1 -10 -7
-3 7 5 6 -3 2
-6 2 -3 9 -7 -5
-5 1 1 -8 6 3
Ms2=randi([-10 10],6)
=
4 -7 -8 -9 -7 1
5 7 10 -2 -5 -7
-1 1 -10 -5 -7 7
-9 10 6 6 -8 3
-6 -9 7 -1 8 -3
9 -1 8 9 2 0
Ms3=randi([-10 10],6)
=
-2 -2 -3 -5 2 -10
-9 -9 8 -2 -9 -7
-5 8 -3 -8 -6 3
-8 9 -8 -8 -3 5
-7 0 6 9 7 3
-5 0 -2 10 -10 -1
[s1 s2]=size(Ms1)
M2=zeros(s1,s2,3);
M2(:,:,1)=Ms1;
M2(:,:,2)=Ms2;
M2(:,:,3)=Ms3;
2.
building the A vector
A=squeeze(M2(1,1,:))'
=
7 4 -2
3.
Calling Msi from workspace
To avoid having to manually call and stack 50 matrices, one can use command who
d2=who('Ms*')
=
3×1 cell array
'Ms1'
'Ms2'
'Ms3'
[sd1 sd2]=size(d2)
sd1 =
3
sd2 =
1
M2=zeros(6,6,sd1)
for k=1:1:sd1
L=['M2(:,:,' num2str(k) ')=' d2{k,:}]
evalin('base',L)
end
Set sd1 to 50 or whatever total amount of Msi matrices in order to automate the stacking of all Msi onto M2
.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
3 Comments
John BG
on 6 Jul 2017
ain't ugly if it works and solves the question
The posted method runs in the command window only due to the evalin('base', ...) command, but an eval instead would solve this. The whos('Ms*') leads to a crash if e.g. the variable "MsInfo" has been created anywhere. Matlab's JIT acceleration does not work efficiently after a dynamic creation of variables, such that the runtime of loops can increase massively.
I find eval -like commands ugly. The community finds them ugly, MathWorks suggests to prefer better methods, the FAQ finds them ugly, there is an exhaustive explanation in the forum of the ugly problems caused by these commands. eval causes more problems than it solves.
A = zeros(50, 1);
for k = 1:50
A(k) = Ms{k}(1,1);
end
Nice.
John BG
on 7 Jul 2017
thanks for pointing out the possibility of evalin not working correctly for variables name starting with Ms but with other spelling than Ms# .. where # is the numeral.
I assumed that in the context of this question, the question originator, Mr Sebastian Neckermann, would make sure that such variable conflicts do not occur.
regards
John BG
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!