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

1 vue (au cours des 30 derniers jours)
Ismail Qeshta
Ismail Qeshta le 19 Fév 2019
Commenté : Ismail Qeshta le 19 Fév 2019
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

Réponse acceptée

Stephen23
Stephen23 le 19 Fév 2019
Modifié(e) : Stephen23 le 19 Fév 2019
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 commentaires
Stephen23
Stephen23 le 19 Fév 2019
"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 le 19 Fév 2019
Thank you very much Stephen. That was really helpful. I just did that and it worked well.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by