MATLAB: I need to save columns from n number of matrices into different variables

for loopiterationMATLABmatrices

Hi,
I have 40 matrices, each 32026×4, and I I need to store each column of each matrix into a different variable. Writing the following is cumbersome and impracticle when I have larger number of matrices:
x1=matrixA(:,1);
y1=matrixA(:,2);
u1=matrixA(:,3);
v1=matrixA(:,4);
As you can see, it would take ages just to write the code. And debuggin will be even worse if I make a mistake somewhere. Do I need some sort of double for loop? Appreciate the help and thanks in advance!

Best Answer

Rather than forcing yourself into writing badly designed code (like your very inefficient eval usage inside the for loop), you should simply import the data into one array, then you can trivially access your imported data using simple, neat, and very efficient indexing:
S = dir('*.txt');
N = numel(S);
C = cell(1,N);
for k = 1:N
C{k} = load(S(k).name,'-ascii')
end
This code follows exactly the examples in the MATLAB documentation:
Most likely you should use a more suitable function for importing text data, e.g. dlmread or csvread or readtable or ...: