加载包含相同变量名的多个 .mat 文件并同时更改变量名?
Loading multiple .mat files containing the same variable name and changing the variable names simultaneously?
所以我有一个包含许多 .mat 文件的目录:
apples.mat, oranges.mat, bananas.mat, grapes.mat, apricots.mat, pears.mat, pineapple.mat
所有这些 .mat 文件都为变量名 "calories" 分配了值。我如何在 MATLAB 中同时加载所有这些 .mat 文件并将每个文件的变量从 calories 更改为 calories_(fruit name) 以便我可以在工作区中使用所有变量值?
例如,我想加载apples.mat并将其变量名从卡路里更改为calories_apple,然后加载oranges.mat并将变量名从calories_orange更改为,等等,无需手动完成。
我知道这类似于创建一个具有不同水果名称的字符串,并沿着字符串建立索引以加载文件并将其变量从 variable 更改为 variable_%s,其中 %s 表示水果含量沿循环生成。这对我来说是一团糟,但是,我认为我的结构不正确。有人愿意帮助我吗?
我会按顺序加载每个 .mat
文件,并将每个相应的卡路里作为一个单独的字段,全部组合成一个 struct
供您访问。给定这些 .mat
文件所在的目录,执行如下操作:
%// Declare empty structure
s = struct()
folder = '...'; %// Place directory here
%// Get all MAT files in directory
f = dir(fullfile(folder, '*.mat'));
%// For each MAT file...
for idx = 1 : numel(f)
%// Get absolute path to MAT file - i.e. folder/file.mat
name = fullfile(folder, f(idx).name);
%// Load this MAT file into the workspace and get the calories variable
load(name, 'calories');
%// Get the name of the fruit, are all of the characters except the last 4 (i.e. .mat)
fruit_name = f(idx).name(1:end-4);
%// Place corresponding calories of the fruit in the structure
s.(['calories_' fruit_name]) = calories;
end
然后您可以像这样使用点符号访问每个卡路里:
c = s.calories_apple;
d = s.calories_orange;
...
...
...等等。
我假设您并不是要在 calories_(fruit name)
中包含括号。我还假设您的当前目录中没有其他 .mat
文件。这应该可以解决问题:
theFiles = dir('*.mat');
for k = 1:length(theFiles)
load(theFiles(k).name, 'calories');
eval(['calories_' theFiles(k).name(1:length(theFiles(k).name)-4) ' = calories;'])
clear calories
end
让我知道这是否有帮助。
编辑
正如,rayryeng 指出的那样。显然,使用 eval 是一种不好的做法。所以,如果你愿意改变你思考问题的方式,我建议你使用结构。在这种情况下,rayryeng 的回答将是一个可以接受的答案,即使它没有直接回答您原来的问题。
所以我有一个包含许多 .mat 文件的目录:
apples.mat, oranges.mat, bananas.mat, grapes.mat, apricots.mat, pears.mat, pineapple.mat
所有这些 .mat 文件都为变量名 "calories" 分配了值。我如何在 MATLAB 中同时加载所有这些 .mat 文件并将每个文件的变量从 calories 更改为 calories_(fruit name) 以便我可以在工作区中使用所有变量值?
例如,我想加载apples.mat并将其变量名从卡路里更改为calories_apple,然后加载oranges.mat并将变量名从calories_orange更改为,等等,无需手动完成。
我知道这类似于创建一个具有不同水果名称的字符串,并沿着字符串建立索引以加载文件并将其变量从 variable 更改为 variable_%s,其中 %s 表示水果含量沿循环生成。这对我来说是一团糟,但是,我认为我的结构不正确。有人愿意帮助我吗?
我会按顺序加载每个 .mat
文件,并将每个相应的卡路里作为一个单独的字段,全部组合成一个 struct
供您访问。给定这些 .mat
文件所在的目录,执行如下操作:
%// Declare empty structure
s = struct()
folder = '...'; %// Place directory here
%// Get all MAT files in directory
f = dir(fullfile(folder, '*.mat'));
%// For each MAT file...
for idx = 1 : numel(f)
%// Get absolute path to MAT file - i.e. folder/file.mat
name = fullfile(folder, f(idx).name);
%// Load this MAT file into the workspace and get the calories variable
load(name, 'calories');
%// Get the name of the fruit, are all of the characters except the last 4 (i.e. .mat)
fruit_name = f(idx).name(1:end-4);
%// Place corresponding calories of the fruit in the structure
s.(['calories_' fruit_name]) = calories;
end
然后您可以像这样使用点符号访问每个卡路里:
c = s.calories_apple;
d = s.calories_orange;
...
...
...等等。
我假设您并不是要在 calories_(fruit name)
中包含括号。我还假设您的当前目录中没有其他 .mat
文件。这应该可以解决问题:
theFiles = dir('*.mat');
for k = 1:length(theFiles)
load(theFiles(k).name, 'calories');
eval(['calories_' theFiles(k).name(1:length(theFiles(k).name)-4) ' = calories;'])
clear calories
end
让我知道这是否有帮助。
编辑 正如,rayryeng 指出的那样。显然,使用 eval 是一种不好的做法。所以,如果你愿意改变你思考问题的方式,我建议你使用结构。在这种情况下,rayryeng 的回答将是一个可以接受的答案,即使它没有直接回答您原来的问题。