How to process multiple dot mat file using for loop

2 vues (au cours des 30 derniers jours)
Yared Daniel
Yared Daniel le 24 Mai 2021
Commenté : Yared Daniel le 24 Mai 2021
Hello every one, please help me in solving this issue
I have thousands of mat files as shown in figure for sample. Each mat file contains a variable called ‘val’ which is 2 by 19000 matrices. What I need is to extract the first raw of ‘val’ of each dot mat file and save it by naming sequentially like “x1, x2, x3……..” Using for loop.
I have done for a single file successfully by the following algorithm
>> clear all
>> load('dist (1).mat')
>> x = val(1,:);
>> save('x1.mat', 'x')
I have attached some of the dot mat files
Thanks so much in advance for your help

Réponses (1)

David Fletcher
David Fletcher le 24 Mai 2021
Wouldn't it be better to extract what you want into a single matrix and then just save the one matrix with all the data rather than having thousands of separate files? Having said that, a basic framework for doing what you want is:
clear
numberOfFiles= %enter number of files;
for iter=1:numberOfFiles
fName=sprintf('dist (%d).mat',iter);
x=load(fName);
x=x.val(1,:);
fName=sprintf('x%d.mat',iter);
save(fName, 'x');
end
  5 commentaires
David Fletcher
David Fletcher le 24 Mai 2021
clear
numberOfFiles= %enter number of files;
for iter=1:numberOfFiles
fName=sprintf('dist (%d).mat',iter);
x=load(fName);
allData{iter}=x.val(1,:)
end
save('data.mat', 'allData.mat')
Yared Daniel
Yared Daniel le 24 Mai 2021
I sincerely appreciate your help thank you so much

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by