Read in files with loop
Show older comments
I have the code below which reads in the data to log200. The thing is, I also need to read in other data too.
I need log200, log225, log250, in increments of 25 up to log900
How can I take my current working script which reads in log200, and replace it with a loop so that I do not have to copy and paste dozens of times?
clear
clc
lastn=1;
log200=importdata('C:/Users/Ben/Desktop/log.200_equil', ' ', 20, 0);
log200_cell = log200.data(:,6);
log200_cell = log200_cell(1:end-lastn,:);
log200_density_average=mean(log200_cell)
8 Comments
Ugh. DO NOT do this!
Dynamically accessing or creating variables names is one way that beginners force themselves into writing slow, complex, buggy code, like your code attempts to do:
Simpler code using indexing or fieldnames would be much more efficient, less buggy, and easier to debug.
DO NOT learn from other beginners who recommend using eval or assignin, this will only force you into writing bad code.
Benjamin Cowen
on 10 Aug 2018
Edited: Benjamin Cowen
on 10 Aug 2018
Stephen23
on 10 Aug 2018
@Benjamin Cowen: how to read files in a loop is shown in the MATLAB documentation:
Note the simple cell array and indexing used to store the data.
Benjamin Cowen
on 10 Aug 2018
Edited: Benjamin Cowen
on 10 Aug 2018
Stephen23
on 10 Aug 2018
@Benjamin Cowen: see my answer.
Benjamin Cowen
on 10 Aug 2018
Benjamin Cowen
on 10 Aug 2018
Edited: Benjamin Cowen
on 10 Aug 2018
Accepted Answer
More Answers (2)
Paul Shoemaker
on 10 Aug 2018
Edited: Paul Shoemaker
on 10 Aug 2018
You could wrap it in a FOR loop and employ the "eval" function.
lastn=1;
clear
clc
for idx = 200:25:900
temp = importdata(['C:/Users/Ben/Desktop/log.' num2str(idx) '_equil'], ' ', 20, 0); % Use num2str to replace "200" with the idx value
temp = temp.data(:,6);
temp = temp(1:end-lastn,:);
eval(['log' num2str(idx) '_density_average=mean(temp);']); % Now evaluate to create desired output
end
In the above, I'm assuming that only the "mean" calculation is desired and everything prior to that is just processing that you can throw away. If not, you can use "eval" on those as well.
Image Analyst
on 10 Aug 2018
0 votes
Do not use eval(). To read in a bunch of files called "log.***" use one of the two code samples in the FAQ: https://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
2 Comments
Benjamin Cowen
on 10 Aug 2018
Edited: Benjamin Cowen
on 10 Aug 2018
Image Analyst
on 10 Aug 2018
It doesn't look like your file pattern is correct, but it looks like you have a working answer from Stephen. However, if you're still interested in using the FAQ code...
I think the file pattern should have been 'log.*' will work for "log.300_equil" if the extension if of the form "nnn_equil".
And 'log*.txt' will work for "log.300_equil.txt" if your files have '.txt' extensions like the one you attached.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!