How Can I Automatically Load .mat Files in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter mathia
  • Start date Start date
  • Tags Tags
    files Matlab
Click For Summary

Discussion Overview

The discussion focuses on methods for automatically loading multiple .mat files in MATLAB, addressing the challenges of handling a large number of files without manually typing each filename. The scope includes practical coding solutions and script automation.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • Mathias inquires about a method to automatically load numerous .mat files into MATLAB without manually entering each filename.
  • One participant suggests using the load command for individual files and mentions placing it in a startup.m file for automatic loading upon starting MATLAB.
  • Mathias clarifies the need for a solution that does not require typing individual filenames due to the large number of files (100,000).
  • Another participant proposes organizing the files in a single directory and using the "what" command to retrieve the filenames, followed by a for loop to load each file iteratively.

Areas of Agreement / Disagreement

Participants present different approaches to the problem, with no consensus on a single solution. The discussion remains open to various methods for automating the loading process.

Contextual Notes

Participants do not specify limitations or assumptions regarding the directory structure or file naming conventions that may affect the proposed solutions.

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