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

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by