Matlab, how read and extract matrices

  • Context: MATLAB 
  • Thread starter Thread starter Valentina*
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary
SUMMARY

The discussion centers on using MATLAB to read and extract matrices efficiently. The user seeks to implement a for-loop to read multiple matrices from specified file paths using the dlmread function. The user attempts to dynamically create variable names for the matrices but encounters issues with string indexing in the loop. The solution involves correctly formatting the loop and using cell arrays for dynamic variable storage.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of matrix operations in MATLAB
  • Knowledge of the dlmread function for reading data files
  • Basic concepts of loops and indexing in programming
NEXT STEPS
  • Learn about using cell arrays in MATLAB for dynamic variable storage
  • Research the correct syntax for sprintf and string interpolation in MATLAB
  • Explore the readmatrix function as an alternative to dlmread
  • Study how to extract specific columns from matrices in MATLAB
USEFUL FOR

Students, researchers, and MATLAB users who need to efficiently read and manipulate multiple matrices in their projects.

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 ·
Replies
7
Views
6K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
7K
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K