Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab, how read and extract matrices

  1. May 25, 2012 #1
    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));

    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!
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted