How to Import Multiple .mat Files with Their Original Names into Workspace

조회 수: 4 (최근 30일)
Ahmet Usta
Ahmet Usta 2018년 9월 4일
댓글: Rishi Binda 2018년 9월 7일
Hi everyone,
I have a group of .mat files that I want to import into the Matlab workspace with their original names. The problem is that when I import for example the file named 'Glass-on-AlSine_Sample1_Offset5V_Amppt1Vto10V_f1Hz_Fn100N_Area10mmx10mm_T1_damping.mat', it shows up in the workspace as 'damping' because 'damping' is the name of the variable that I used in the function while generating that .mat file. Can you please suggest me a way that I can use to import multiple .mat files while preserving their names?
Thank you all in advance.
  댓글 수: 5
Ahmet Usta
Ahmet Usta 2018년 9월 4일
Hypens is not a must for me. I can have the names of the variables like in the following as well:
Sample1_Offset5V_Amppt1Vto10V_f1Hz_Fn100N_Area10mmx10mm_T1_damping Sample1_Offset5V_Amppt1Vto10V_f1Hz_Fn100N_Area10mmx10mm_T1_energy
Thanks.
Stephen23
Stephen23 2018년 9월 7일
편집: Stephen23 2018년 9월 7일
"Can you please suggest me a way that I can use to import multiple .mat files while preserving their names?"
No, this is not possible. There are many filenames that are not valid variable names, e.g. your own example shows hyphens, which are not allowed. So you would need to alter the names before using them. You would also have to include something to deal with potential clashes, e.g. A-1.mat and A+1.mat would have to be changed into two unique names, at which point you have lost any useful connection between the filenames and the variable names.
But the whole idea is a red herring anyway, as putting meta-data into variable names just forces beginners into writing slow, complex, buggy code. Meta-data is data, which means that it should be stored as data in its own right, not forced awkwardly into variable names.
By putting meta-data into the variable names you are forcing your self into writing slow, complex, code. You make processing your data much harder than it needs to be. Read this to know more:

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

답변 (1개)

Rishi Binda
Rishi Binda 2018년 9월 7일
You can change the name of the variable to the name of the mat-file before creating it but I believe that is not what you would prefer.
The workspace will display the variables names when you load a mat-file. So the only way is to change the variable name inside the mat-file. I can suggest a way to do this. Note that only the variable name inside the mat-file will change and not the original variable you used in your function.
Here is a sample script which changes the name of variable 'a' to 'xx' which is the name of the mat-file.
a=randn(5);
save('xx.mat','a');
var=load('xx.mat');
var=cell2struct(struct2cell(var), {'xx'});
save('xx.mat','-struct','var');
load('xx.mat');
Loads the mat file in a structure 'var' which makes the variables as fields of 'var'. Rename the variable(s) as the mat-file name and save the structure. Now when you load the mat-file you will see the updated variable name in the workspace.
For multiple files you can write a script which extracts the filenames of the files with .mat extension as a string array. Use the string array to access and edit mat-files in the code.
Hope this solves your issue!
  댓글 수: 2
Stephen23
Stephen23 2018년 9월 7일
편집: Stephen23 2018년 9월 7일
This perfectly illustrates why magically accessing variable names is slow and complex: the data is transferred to/from the drive four times, which is going to be a very slow process.
Better, simpler, more efficient code would simply process all of the files using one simple loop, store the meta-data as data in its own right, and access it trivially using indexing/fieldnames/etc.
Rishi Binda
Rishi Binda 2018년 9월 7일
I had the same thought when I read it. The data read/write is being repeated multiple times. The user can simply give valid and suitable variable names before storing them in mat files. I assumed the user wants to keep the original variable names as it is. May be because of readability or further use of variables. Renaming them would indeed simplify the process.

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by