Automatically naming mat files with variable names

31 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2020-9-21
编辑:Stephen23 2020-9-21
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 个评论
Kirsten Schoonover
Kirsten Schoonover 2020-9-21
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.

请先登录,再进行评论。

更多回答(1 个)

Adam Danz
Adam Danz 2020-9-21
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 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2020-9-21
You can also use sprintf to generate filenames with numbered filenames, like img_p12-01.mat.
Adam Danz
Adam Danz 2020-9-21
编辑:Adam Danz 2020-9-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by