How do I load a .mat-file directly into a table and give this table the same name as the .mat-file?
32 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
dpb
2022 年 6 月 12 日
"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
2022 年 6 月 12 日
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.
回答 (1 件)
Pooja Kumari
2023 年 11 月 17 日
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 件のコメント
Stephen23
2023 年 11 月 17 日
編集済み: Stephen23
2023 年 11 月 17 日
"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.
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!