在 MATLAB 中动态命名结构变量
Dynamically name a struct variable in MATLAB
我有几个文件“2011-01-01.txt”、“2013-01-02.txt”、“2015-02-01.txt”等。
我希望为每个文件创建一个结构变量,这样(值由)组成:
machine20110101.size=[1,2,3];
machine20110101.weight=2345;
machine20110101.price=3456;
machine20130102.size=[2,3,4];
machine20130102.weight=1357;
machine20130102.price=123;
machine20150201.size=[1,2,4];
machine20150201.weight=1357;
machine20150201.price=123;
并且,
save('f20110101.mat','machine20110101');
save('f20130102.mat','machine20130102') ;
save('f20150201.mat','machine20150201');
正如我们所见,结构名称是从文件名称派生而来的。如何构造上述结构变量?
找了一段时间,没搞清楚怎么用genvarname
。
这些链接 (, dynamic variable declaration function matlab, Dynamically change variable name inside a loop in MATLAB) 没有解决我的问题。
我使用的是MATLAB R2012b,所以此版本之后定义的matlab.lang.makeUniqueStrings
等函数不可用。
既然我在 MATLAB 面前,下面是一个基于我上面评论的示例,使用 dynamic field names with the filenames pruned using fileparts
and regexprep
in a cellfun
调用。
% Sample list for testing here, use uigetdir with dir or whatever method to
% get a list of files generically
filelist = {'C:\mydata11-01-01.txt', ...
'C:\mydata12-02-02.txt', ...
'C:\mydata13-03-03.txt', ...
'C:\mydata14-04-04.txt', ...
};
nfiles = length(filelist);
% Get filenames from your list of files
[~, filenames] = cellfun(@fileparts, filelist, 'UniformOutput', false);
% Prune unwanted characters from each filename and concatenate with 'machine'
prunedfilenames = regexprep(filenames, '-', '');
myfieldnames = strcat('machine', prunedfilenames);
% Generate your structure
for ii = 1:nfiles
% Parse your files for the data, using dummy variables since I don't
% know how your data is structured
loadedsize = [1, 2, 3];
loadedweight = 1234;
loadedprice = 1234;
% Add data to struct array
mydata.(myfieldnames{ii}).size = loadedsize;
mydata.(myfieldnames{ii}).weight = loadedweight;
mydata.(myfieldnames{ii}).price = loadedprice;
end
@patrik 在评论中提出了一些好的观点。我认为他希望看到的更通用的方法(如果我错了请纠正我)是这样的:
% Sample list for testing here, use uigetdir with dir or whatever method to
% get a list of files generically
filelist = {'C:\mydata11-01-01.txt', ...
'C:\mydata12-02-02.txt', ...
'C:\mydata13-03-03.txt', ...
'C:\mydata14-04-04.txt', ...
};
nfiles = length(filelist);
% Get filenames from your list of files
[~, filenames] = cellfun(@fileparts, filelist, 'UniformOutput', false);
% Prune unwanted characters from each filename and concatenate with 'machine'
prunedfilenames = regexprep(filenames, '-', '');
mytags = strcat('machine', prunedfilenames);
% Preallocate your structure
mydata = repmat(struct('tag', '', 'size', [1, 1, 1], 'weight', 1, 'price', 1), nfiles, 1);
% Fill your structure
for ii = 1:nfiles
% Parse your files for the data, using dummy variables since I don't
% know how your data is structured
loadedsize = [1, 2, 3];
loadedweight = 1234;
loadedprice = 1234;
% Add data to struct array
mydata(ii).tag = mytags{ii};
mydata(ii).size = loadedsize;
mydata(ii).weight = loadedweight;
mydata(ii).price = loadedprice;
end
除了@excaza的回答,我还使用了以下方法:
machine.size = [1,2,3]; machine.price = 335; machine.weight = 234;
machineName = ['machine',the_date];
machineSet = struct(machineName,machine);
save(OutputFile,'-struct','machineSet',machineName);
我有几个文件“2011-01-01.txt”、“2013-01-02.txt”、“2015-02-01.txt”等。 我希望为每个文件创建一个结构变量,这样(值由)组成:
machine20110101.size=[1,2,3];
machine20110101.weight=2345;
machine20110101.price=3456;
machine20130102.size=[2,3,4];
machine20130102.weight=1357;
machine20130102.price=123;
machine20150201.size=[1,2,4];
machine20150201.weight=1357;
machine20150201.price=123;
并且,
save('f20110101.mat','machine20110101');
save('f20130102.mat','machine20130102') ;
save('f20150201.mat','machine20150201');
正如我们所见,结构名称是从文件名称派生而来的。如何构造上述结构变量?
找了一段时间,没搞清楚怎么用genvarname
。
这些链接 (
我使用的是MATLAB R2012b,所以此版本之后定义的matlab.lang.makeUniqueStrings
等函数不可用。
既然我在 MATLAB 面前,下面是一个基于我上面评论的示例,使用 dynamic field names with the filenames pruned using fileparts
and regexprep
in a cellfun
调用。
% Sample list for testing here, use uigetdir with dir or whatever method to
% get a list of files generically
filelist = {'C:\mydata11-01-01.txt', ...
'C:\mydata12-02-02.txt', ...
'C:\mydata13-03-03.txt', ...
'C:\mydata14-04-04.txt', ...
};
nfiles = length(filelist);
% Get filenames from your list of files
[~, filenames] = cellfun(@fileparts, filelist, 'UniformOutput', false);
% Prune unwanted characters from each filename and concatenate with 'machine'
prunedfilenames = regexprep(filenames, '-', '');
myfieldnames = strcat('machine', prunedfilenames);
% Generate your structure
for ii = 1:nfiles
% Parse your files for the data, using dummy variables since I don't
% know how your data is structured
loadedsize = [1, 2, 3];
loadedweight = 1234;
loadedprice = 1234;
% Add data to struct array
mydata.(myfieldnames{ii}).size = loadedsize;
mydata.(myfieldnames{ii}).weight = loadedweight;
mydata.(myfieldnames{ii}).price = loadedprice;
end
@patrik 在评论中提出了一些好的观点。我认为他希望看到的更通用的方法(如果我错了请纠正我)是这样的:
% Sample list for testing here, use uigetdir with dir or whatever method to
% get a list of files generically
filelist = {'C:\mydata11-01-01.txt', ...
'C:\mydata12-02-02.txt', ...
'C:\mydata13-03-03.txt', ...
'C:\mydata14-04-04.txt', ...
};
nfiles = length(filelist);
% Get filenames from your list of files
[~, filenames] = cellfun(@fileparts, filelist, 'UniformOutput', false);
% Prune unwanted characters from each filename and concatenate with 'machine'
prunedfilenames = regexprep(filenames, '-', '');
mytags = strcat('machine', prunedfilenames);
% Preallocate your structure
mydata = repmat(struct('tag', '', 'size', [1, 1, 1], 'weight', 1, 'price', 1), nfiles, 1);
% Fill your structure
for ii = 1:nfiles
% Parse your files for the data, using dummy variables since I don't
% know how your data is structured
loadedsize = [1, 2, 3];
loadedweight = 1234;
loadedprice = 1234;
% Add data to struct array
mydata(ii).tag = mytags{ii};
mydata(ii).size = loadedsize;
mydata(ii).weight = loadedweight;
mydata(ii).price = loadedprice;
end
除了@excaza的回答,我还使用了以下方法:
machine.size = [1,2,3]; machine.price = 335; machine.weight = 234;
machineName = ['machine',the_date];
machineSet = struct(machineName,machine);
save(OutputFile,'-struct','machineSet',machineName);