有几个.dat文件,我必须运行一个matlab函数。
考虑四个文件:
,我希望按字母顺序读取文件,因为每个文件都有一个特定的参数传递给从excel表中选择的函数。
问题--下面是我编写的代码,但是MATLAB没有按顺序读取文件(例如,MATLAB文件读取顺序是1,4,2,3,在上面的编号列表中给出的指数化之后),因此与每个文件相关的参数被错误地传递了。
filename = 'C:\Book1.xlsx';
freq = xlsread(filename, 'A1:A4');
myFolder = 'C:\ZynqNew';
% Check to make sure that folder actually exists.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.dat');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
maxVal=hppFunction(fullFileName,(1),freq(k),1,1,1,1,1,1);
arrayPeak(k)=maxVal;
end
有人能告诉我我犯了什么错误吗?
发布于 2019-08-12 12:46:30
由于您没有使用前导零,所以Matlab使用的顺序是正确的字母顺序(由dir
命令给出)。您可以使用例如Matlab中的sort_nat
来解决这个问题:
filePattern = fullfile(myFolder, '*.dat');
theFiles = dir(filePattern);
theFilesNatOrder = nat_sort({theFiles.name});
for k = 1 : length(theFilesNatOrder)
baseFileName = theFilesNatOrder{k};
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
maxVal=hppFunction(fullFileName,(1),freq(k),1,1,1,1,1,1);
arrayPeak(k)=maxVal;
end
https://stackoverflow.com/questions/57461164
复制相似问题