Automatically naming mat files with variable names

39 visualizzazioni (ultimi 30 giorni)
I am fairly new to matlab, and have not been able to figure this out for the life of me. I have several thousand images that each have six variables that I need to save to individual mat files. My workflow currently involves importing each image's six variables into matlab, then using
c=who;
save(['H:\My_Directory\Mat_Files\' name])
where "name" is manually entered each time. However, manually entering the name for upwards of 16,000 images sounds absolutely awful. Is there a way to name the mat file after a name-changing variable I imported into matlab without having to manually enter the name? Or name the file with information contained within variable c?
To be clear, I'm not running a loop in matlab, I'm using Slidebook (image capturing and processing software, where my images are stored) to perform the "loop"ing aspect, simply because I have to pull each of the 6 variables from each image contained within slidebook. The script listed above is the only code that runs in matlab itself.
so my workspace contains variables:
  • Cell
  • x488
  • x568
  • x647
  • x750
  • xLipo
  • x5_1_1_0
Where variable x5_1_1_0 is the image name and will change with each image. c=who contains all of these variable names.
Any and all help would be greatly appreciated, I am super stuck at the moment.

Risposta accettata

Stephen23
Stephen23 il 21 Set 2020
Modificato: Stephen23 il 21 Set 2020
The cause of your difficulties lies in the unfortunate design decision to force meta-data into the variable names. Meta-data is data, and data should be stored in a variable, not in the variable's name:
Unfortunately badly written code does this, even sometimes code from apparently reputable third-party sources.
If you only have those seven variables in the workspace then most likely you can identify the "name" by some feature, e.g. assuming that the "name" is the only string to contain the '_' underscore character:
wsp = who();
dst = 'H:\My_Directory\Mat_Files';
idx = ~cellfun(@isempty,regexp(wsp,'_'));
assert(nnz(idx)==1,'zero or multiple matches!')
save(fullfile(dst,wsp{idx}), wsp{:})
Note the comma-separated list telling save which variables to save:
  1 Commento
Kirsten Schoonover
Kirsten Schoonover il 21 Set 2020
THIS WORKED BEAUTIFULLY THANK YOU. I will absolutely take your advice about the badly written code and try to avoid that in the future. Again, thank you so much, you have saved me thousands of hours.

Accedi per commentare.

Più risposte (1)

Adam Danz
Adam Danz il 21 Set 2020
Use fullfile to define the full path and file name.
destination = 'H:\My_Directory\Mat_Files';
filename = fullfile(destination, [x5_1_1_0,'.mat']);
Then save all variables or a list of variables using,
or
  2 Commenti
Bjorn Gustavsson
Bjorn Gustavsson il 21 Set 2020
You can also use sprintf to generate filenames with numbered filenames, like img_p12-01.mat.
Adam Danz
Adam Danz il 21 Set 2020
Modificato: Adam Danz il 21 Set 2020
Good idea, Bjorn Gustavsson .
fname = sprintf('%s_%d.mat', x5_1_1_0, 1); % where '1' is the numeric tag
filename = fullfile(destination, fname);
@Kirsten Schoonover, also heed Stephen Cobeldick's advice. Using a preexisting string as a file name is OK but if you're dynamically naming that string you could be entering into some pitfalls.

Accedi per commentare.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by