How to sort the results correctly in this "for" loop?
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I need to find the Y value that corresponds to the maximum X value for the curves attached herewith for 10 files.
I am using the code given below, but the output starts with file1, file10, file2, file3, ..., file9. It should be file1, file2, file3, .., file10.
Can anyone please help me with this?
Thank you.
files = dir('*.out') ;
N = length(files) ;
iwant = zeros(N,2) ;
for i = 1:N
data = load(files(i).name) ;
[val,idx] = max(data(:,1)) ;
iwant(i,:) = [val data(idx,2)] ;
end
0 个评论
采纳的回答
Stephen23
2019-2-19
编辑:Stephen23
2019-2-19
Simply download my FEX submission natsortfiles:
And follow the examples in its documentation, e.g.:
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
Z = zeros(N,2);
for k = 1:N
data = load(C{k});
[val,idx] = max(data(:,1));
Z(k,:) = [val,data(idx,2)];
end
5 个评论
Stephen23
2019-2-19
"I opened that page and downloaded a zip file that contains one matlab file and one text file."
Then you did not download from the page that I linked to: the zip file contains three .m files and one .txt file (I just checked this now).
Once you download the zip file from the page that I gave you, then the zip file contains everything that you need. Simply unzip it and put the .m files into your current directory (or somewhere on your MATLAB path) and you are ready to use them (exactly as I showed in my answer). You do not need to set up anything or "run" anything to use them, they are quite normal MATLAB functions that you can use straight away just by calling them exactly like you would call any other MATLAB function.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!