How to modify this could so it can read from different files groups?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi,
I have this code that reads from 10 files each maximum X value and its corresponding Y value and prints all results in one file (named "Result_1, Result_2,...")
Now, I have 400 files. I need to modify the code, so it can read the different groups sequentially and print the output file for each group.
For instance, I need it to read from 1 to 10, then from 11 to 20, then from 21 to 30, and so on until 391 to 400.
Hence, the printed output files are: Result_1 (for files 1 to 10), Result_2 (for files 11 to 20), and so on until the output file Result_40 (for files 391 to 400).
The code:
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
Z = nan(N,1);
for k = 1:N
data = load(C{k});
[~,idx] = max(data(:,1));
Z(k) = data(idx,2);
end
dlmwrite('Result_1.txt',Z)
Accepted Answer
Bob Thompson
on 20 Feb 2019
Add a second loop. Instead of just going through all files in a single loop, have an outer loop that looks for the number of groups you have, and an inner loop that has the number of files in each group. The example below assumes you have a constant number of files in each group.
S = dir('*.out');
C = natsortfiles({S.name});
N = numel(C);
g = 10;
Z = nan(g,1);
for k = 1:N/g
for j = 1:g;
data = load(C{10*(k-1)+j});
[~,idx] = max(data(:,1));
Z(j) = data(idx,2);
end
dlmwrite(['Result_',num2str(k),'.txt'],Z);
end
10 Comments
Ismail Qeshta
on 20 Feb 2019
Hi Bob,
Many thanks for your reply and suggesting the code.
Actually, the output is only 4 files of "Result_", while they should be 40 files.
Bob Thompson
on 20 Feb 2019
What is your value of N? If it is 400 then you should be getting 40 files with this code, as k should loop from 1:40. If this is not happening then what is the maximum k value you are getting?
Ismail Qeshta
on 20 Feb 2019
Edited: Ismail Qeshta
on 20 Feb 2019
Hi Bob,
Many thanks for your clarification. Yes, the code works now.
I just dropped an inquiry in the previous question for the other code regarding rounding the data imported from a file. It can be found at:
I just need to resolve the accuracy of that code in order to make sure that this code works well.
Ismail Qeshta
on 20 Feb 2019
Hi Bob,
Thank you for the code.
Would it be possible to expand this code to 8000 instead of 400 data? or any other number?
This is because I may need to expan it later.
Bob Thompson
on 20 Feb 2019
Yes, it is very expandable. N will adjust automatically because it is just dependant on the data you read in. You will need to set g manually, but it will easily adjust the number of datasets you include in one file.
Ismail Qeshta
on 20 Feb 2019
Thank you Bob. Do you mean, for instance, if I have 8000 data, then g would be 100? I mean, how would the value of g change as the number of data (N) change?
Bob Thompson
on 20 Feb 2019
g does not change reactively to N. If you want g to be 100, then you have to set g = 100; within the code, as that is how the code is currently set up.
Ismail Qeshta
on 20 Feb 2019
I see. So, g refers to the file groups to be read and printed in each output file, is that correct?
Bob Thompson
on 20 Feb 2019
g refers to the number of sets of data to be printed into each output. So, I think we are saying the same thing, yes.
Ismail Qeshta
on 21 Feb 2019
Thank you very much Bob for all your time and help.
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)