MATLAB: How do you use one vector to name another vector

assignin

If you have three vectors: vect1 = [1 2], vect2 = [4 5], and vect3 = {'name1';'name2'}, how do I name vect1 and vect2 using the contents of vect3, i.e., how do I obtain
name1 = [1 2]
name2 = [4 5].
I have found one solution where the function assignin is used but I was wondering if there may be a more streamlined/simpler solution?

Best Answer

What you want to do has to be near the top of everyone’s ‘Don’t Do This!’ list.
But if you must, this works:
vect1 = [1 2];
vect2 = [4 5];
vect3 = {'name1';'name2'};
for k1 = 1:length(vect3)
eval([vect3{k1} sprintf(' = vect%-d',k1)]);
end
name1
name2