首先,您需要生成文件列表。我有我自己的函数,但是有,例如,GETFILELIST或优秀的交互式UIPICKFILES来生成文件列表。
一旦你有了文件列表(我假设它是一个包含文件名的元胞数组),你可以执行以下操作:
nFiles = length(fileList);
Master_matrix = zeros(7,N);
for iFile = 1:nFiles
%# if all files contain a variable of the same name,
%# you can simplify the loading by not assigning an output
%# in the load command, and call the file by
%# its variable name (i.e. replace 'loadedData')
tmp = load(fileList{iFile});
fn = fieldnames(tmp);
loadedData = tmp.(fn{1});
%# find size
w = size(loadedData,2);
if w>=N
Master_matrix = Master_matrix + loadedData(:,1:N);
else
%# only adding to the first few columns is the same as zero-padding
Master_matrix(:,1:w) = Master_matrix(:,1:w) = loadedData;
end
end
注意:如果您实际上不想将数据相加,而只是将其存储在主数组中,则可以将Master_matrix
其制成一个 7×N×nFiles 数组,其中的第 n 个平面Master_matrix
是第 n 个文件。在这种情况下,您将初始化Master_matrix
为
Master_matrix = zeros(7,N,nFiles);
你会把 if 子句写成
if w>=N
Master_matrix(:,:,iFile) = Master_matrix(:,:,iFile) + loadedData(:,1:N);
else
%# only adding to the first few columns is the same as zero-padding
Master_matrix(:,1:w,iFile) = Master_matrix(:,1:w,iFile) = loadedData;
end
另请注意,您可能希望初始化Master_matrix
为NaN
而不是zeros
,以便零不会影响后续统计信息(如果这是您想要对数据执行的操作)。