MATLAB How Can I Automatically Load .mat Files in MATLAB?

  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    files Matlab
AI Thread Summary
To automate the loading of multiple *.mat files in MATLAB, the recommended approach involves using the "what" command to retrieve all filenames in a specified directory. By executing "K = what(directory)", a structure containing the filenames is created. A for loop can then be utilized to iterate through each file, allowing for the loading of each one without manually typing the names. The loop structure would be "for i = 1:length(K.mat)", followed by "load(K.mat{i})" to load each file and perform any necessary operations. This method is efficient for handling a large number of files, such as 100,000.
mathia
Messages
15
Reaction score
0
Hi,

I have some *.mat files and I need to read them automatically, they are like:
PT2020-RSK1.778-ZONE49.8962.mat
PT2020-RSK2.776-ZONE47.5451.mat
PT2020-RSK3.776-ZONE46.041.mat
PT2020-RSK4.7695-ZONE56.5417.mat
PT2020-RSK5.7667-ZONE48.1113.mat

Are there any ways to load them into MATLAB automatically?

Thanks for your time.

Regards,
Mathias
 
Physics news on Phys.org
You can use the load command. If you want them loaded from a script try something like
Code:
load PT2020-RSK1.778-ZONE49.8962.mat;
if you want them to always be in your workspace you can put the load command in your startup.m.
 
Thanks, But I want to load them automatically, I don't want to type the name of each individual mat file because I have 100000 mat files.
 
Put them in all in one directory then and play with the "what" command. So you'd do something like:

K = what(directory)

And now K will be a structure with the filenames, then you can just go through each K.mat with a for loop

for example:

for i = 1:length(K.mat)

load(K.mat{i})

(do stuff with it)

end
 

Similar threads

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