How to sort the results correctly in this "for" loop?

2 次查看(过去 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

采纳的回答

Stephen23
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
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.
Ismail Qeshta
Ismail Qeshta 2019-2-19
Thank you very much Stephen. That was really helpful. I just did that and it worked well.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by