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

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

edit: would the answer to this question also work with csv-files using xlsread?
Trust me, you don't want to do this -- create a variable called "LCN_10".
People ask for this nearly every day and the answer is always the same: "Don't do it".
Someone (Stephen) will soon give you plenty of links saying why not.
As far as assigning data to a table on the GUI, set the .Data property of the table to your cell array or numerical array of data.
@Leo Hastrich: Trust Image Analyst. Creating variables dynamically let the code become slow and complicated. Do not store important information in the name of variables, but as data.
name = 'LCN_10';
T.data = load([name, '.mat']);
T.name = name;
Now you can let T be an array and search for specific names easily including checks of not existing names. Alternative:
T.(name) = load([name, '.mat']);
But even then it looks, like "LCN_10, LCN_15, LCN_30" contain hidden values in text format. What about:
value = 10;
T.data = load(sprintf('LCN_%d.mat', value);
T.value = value;
"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!"
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개)

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

"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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2022년 6월 12일

편집:

2023년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by