MATLAB Matlab, how read and extract matrices

  • Thread starter Thread starter Valentina*
  • Start date Start date
  • Tags Tags
    Matlab Matrices
AI Thread Summary
The discussion revolves around a user seeking help with MATLAB for reading and extracting multiple matrices efficiently using a for-loop. The user attempts to read six matrices using a loop but struggles with correctly referencing the matrix names and extracting specific columns. They provide an example of their code but encounter errors related to string formatting and variable naming. Additional clarification is needed regarding the source of the matrices to provide a more accurate solution. The conversation highlights common challenges faced by beginners in MATLAB programming.
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
Valentina* said:
I have to read six matrices
Read from where?

Some important questions remain unanswered, so it's not possible to post a good answer.
 

Similar threads

Replies
7
Views
6K
Replies
3
Views
1K
Replies
3
Views
2K
Replies
4
Views
3K
Replies
1
Views
1K
Replies
9
Views
5K
Replies
4
Views
2K
Replies
3
Views
3K
Back
Top