Read in files with loop
36 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Benjamin Cowen
il 10 Ago 2018
Commentato: Image Analyst
il 10 Ago 2018
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 Commenti
Risposta accettata
Stephen23
il 10 Ago 2018
Modificato: Stephen23
il 10 Ago 2018
Simpler to use indexing and fullfile:
D = 'C:/Users/Ben/Desktop/';
S = dir(fullfile(D,'*_equil')); % where is the file extension?
N = numel(S);
C = cell(1,N);
M = nan(1,N);
for k = 1:N
F = fullfile(D,S(k).name);
fprintf('Now reading %s\n',F);
T = importdata(F,' ',20);
C{k} = T.data;
M(k) = mean(T.data(1:end-1,6));
end
All data will be in cell array C, and M should be all of the mean data joined together into one numeric vector. You will need to check the options for importdata: I don't have this so I can't run this code!
"... then take the average of the last 100 points in the 6th column"
If you really want the last 100 points, then perhaps you will need this:
D{k} = mean(T.data(end-100:end-1,6));
7 Commenti
Stephen23
il 10 Ago 2018
@Benjamin Cowen: any time, it was a pleasure! It is nice working with people who show an interest, and who actually respond to our replies :)
Good luck with the code and come and ask us anything!
Più risposte (2)
Paul Shoemaker
il 10 Ago 2018
Modificato: Paul Shoemaker
il 10 Ago 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.
0 Commenti
Image Analyst
il 10 Ago 2018
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 Commenti
Image Analyst
il 10 Ago 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.
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!