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

4 views (last 30 days)
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 Comments
Ahmet Usta
Ahmet Usta on 4 Sep 2018
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 on 7 Sep 2018
Edited: Stephen23 on 7 Sep 2018
"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:

Sign in to comment.

Answers (1)

Rishi Binda
Rishi Binda on 7 Sep 2018
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 Comments
Stephen23
Stephen23 on 7 Sep 2018
Edited: Stephen23 on 7 Sep 2018
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 on 7 Sep 2018
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!