Automatically naming mat files with variable names
Show older comments
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.
Accepted Answer
More Answers (1)
Adam Danz
on 21 Sep 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 Comments
Bjorn Gustavsson
on 21 Sep 2020
You can also use sprintf to generate filenames with numbered filenames, like img_p12-01.mat.
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.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!