How do I make matlab read the files in order?
9 次查看(过去 30 天)
显示 更早的评论
can you please help me how to sort the files? My file names are t-0.dat, t-1.dat,t-2.dat,t-4.dat,...t-16.dat,...., t-128.dat,........t-10000.dat. But when I'm using the code to plot the results, it reads t-0, t-1,t-128,t-10000, then t-2,....t-4... like this. I want to get them in order. How do I do it? I have tried using natsortfiles() but I think I am not able to implement it properly.
myfolder= '/..................../datafolder';
datfiles = dir('*.dat');
for k = 1 : length(datfiles)
baseFilename= datfiles(k).name;
data = load(datfiles(k).name);
fullFilename= fullfile(myfolder, baseFilename);
fprintf(1, 'Now reading %s\n', fullFilename);
h=figure;
scatter(data(:,1),data(:,2),50);
hold on;
quiver(data(:,1),data(:,2),data(:,4),data(:,5),0.3);
% saveas(h,sprintf('fig%d.png',k));
hold off;
end
0 个评论
采纳的回答
Guillaume
2017-5-22
" I have tried using natsortfiles() but I think I am not able to implement it properly."
Something like this should work:
myfolder= '/..................../datafolder';
datfiles = dir('*.dat');
baseFileNames = natsortfiles({datfiles.name});
for k = 1 : numel(baseFileNames)
fullFilename= fullfile(myfolder, baseFileNames{k});
data = load(fullFilename);
fprintf(1, 'Now reading %s\n', fullFilename);
%...
4 个评论
Stephen23
2023-2-20
Note that NATSORTFILES() also directly accepts the structure returned by DIR(), e.g.:
datfiles = dir('*.dat');
datfiles = natsortfiles(datfiles);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!