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

조회 수: 1 (최근 30일)
Ismail Qeshta
Ismail Qeshta 2019년 2월 19일
댓글: Ismail Qeshta 2019년 2월 19일
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에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by