How do I load a .mat-file directly into a table and give this table the same name as the .mat-file?

32 Ansichten (letzte 30 Tage)
I have a folder full of .mat-files with different names (LCN_10, LCN_15, LCN_30, etc.). In my code I want to load these files and read them into tables.
I know the "classic" way by just typing:
T = load('LCN_10.mat')
But this saves it as the workspace variable T. How do I save it as "LCN_10" without typing it manually?
files = dir('*.mat');
for i=1:length(files)
[~,filename] = fileparts(files(i).name);
end
With this code I managed to save the name of the imported file in the character "filename". How do I load the mat-file into a table and give the table the same name as the loaded file?
  5 Kommentare
dpb
dpb am 12 Jun. 2022
"But even then it looks, like "LCN_10, LCN_15, LCN_30" contain hidden values in text format. What about:..."
Instead, what about a slight modification to the original...
baseRootFolder='YourWorkingDataFolder'; % uigetdir to set conveniently maybe?
baseRootName='LCN_';
d=dir(fullfile(baseRootFolder,[baseRootName '*.mat']); % build directory list
for i=1:numel(d)
T(i)=load(fullfile(d(i).folder,d(i).name));
end
which gives you an array of T; each of which contains the variables in the respective .mat file whose names are retrievable by code.
You could add some additional information there as well such as @Jan suggests but whatever you end up choosing, do NOT poof variables into the workspace as your initial request -- "There be dragons!"
Stephen23
Stephen23 am 12 Jun. 2022
Filenames are meta-data. Meta-data is data. Data is efficently and neatly stored in variables, not in variable names.
If you continue to mix up (meta-)data and code then you will force yourself into writing slow, complex, inefficient code. Avoid.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Pooja Kumari
Pooja Kumari am 17 Nov. 2023
Dear Leo,
It is my understanding that you wanted to know how to load the mat-file into a table and give the table the same name as the loaded file. You can use "eval" to create a variable with the same name as the loaded file and assign the loaded data to it. Please refer to the following documentation for more information on “eval” function:
Below is the code for your reference:
files = dir('*.mat');
for i = 1:length(files)
[~, filename] = fileparts(files(i).name)
data = load(files(i).name);
eval([filename ' = struct2table(data);'])
end
I hope this helps!
Regards,
Pooja Kumari
  1 Kommentar
Stephen23
Stephen23 am 17 Nov. 2023
Bearbeitet: Stephen23 am 17 Nov. 2023
"Please refer to the following documentation for more information on “eval” function:"
And also to this documentation:
which clearly states "Although the eval function is very powerful and flexible, it is not always the best solution to a programming problem. Code that calls eval is often less efficient and more difficult to read and debug than code that uses other functions or language constructs...For many common uses of eval, there are preferred alternate approaches..."
The preferred approach to forcing meta-data into variable names is to store meta-data in variables, not in variable names. Then you can write neat, simple, efficient, robust, much better MATLAB code.
For example, the code given by Pooja Kumari is fundamentally fragile/buggy. Consider what their code would do with these perfectly valid filenames:
"1.mat"
"1-2.mat"
"A&B.mat"
"my data.mat"
"2023-11-17 data.mat"
Best avoided.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by