Matlab, how read and extract matrices

  • Context: MATLAB 
  • Thread starter Thread starter Valentina*
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
Valentina*
Messages
1
Reaction score
0
Hello,
I'm new here and in matlab, so if I do mistakes, please, tell me.
I'm working on my thesis and I need to extract some matrices and elements, but I can't write the for-loop exactly. For example, I have to read six matrices (but then I'll have more matrices) and this is too long

matrix_1 = dlmread('/scratch/matrix_1');
matrix_2 = dlmread('/scratch/matrix_2');
matrix_3 = dlmread('/scratch/matrix_3');
...
...
and so on. I'd like to do a for loop, to read the matrices, but I don't know how. I tried with something like

number = [1, 2, 3, 4, 5, 6];
for ii = 1 : 6
tmp = dlmread(sprintf('/scratch/matrix_$i', number[ii]));
eval(sprintf('Matrix_final_$i = tmp', ii));
end

but it doesn't work :( In the same way, after, I have to extract the second column of each matrix, because is too long to do

n1 = matrix_1(:,2);
n2 = matrix_2(:,2);
...
...

I don't understand how to use the index ii in the for loop like a string in the name of the matrices. Does somebody know if it's possible?

Sorry for my English,
thank you!
 
Physics news on Phys.org