Azzera filtri
Azzera filtri

Reading and Processing Many Text Files

20 visualizzazioni (ultimi 30 giorni)
Hi,
I am working on a project that uses Fortran to perform high intensity calculations and writes the results as text files, at certain time step intervals. To get a sense of how the properties of my simualted domain change over time, the Fortran code creates results (.txt) files every X amount of time steps. The files are labelled RESULTS_ONE_XX and RESULTS_TWO_XX, where XX is a number between 15 and 99.
I am using MATLAB to postprocess and visualise the data, particularly the contour function. I have sucessfully made MATLAB read and process a single file, using the file directory, fopen and textscan (working code below).
What I would like to do is to make MATLAB read, for example, RESULTS_ONE_15, then 16, 17 ... 99. Is there a way to use a For loop to automate the reading of the files? I also want to generate contour plots at every results time step, which can be overwritten (creating somewhat of an animation). I can figure that part out, but I am having difficulty attempting to automate the reading process.
My main issue currently is trying to figure out how to make the file directory dynamic but still valid - I've tried something along the lines of
for i=1:X
fid1 = fopen('C:*****\RESULTS_ONE_(i).txt')
end
But obviously that doesn't work - no surprises there! Would I need to make dynamic variables too? I've read that that is never a great idea but if it is the only option I would definitely consider it. Either way, I still don't know what to put in the file directory!
If anyone has any advice then that would be greatly appreciated.
Many Thanks,
JP
*** WORKING CODE BELOW ***
fid1 = fopen('C:***FILE LOCATION***\RESULTS_ONE_15.txt'); % OPEN FILE
A = textscan(fid1,'%f %*f %*f %*f %*f'); % Read First Column
Col_1 = cell2mat(A); % Convert to List
List_1 = Col_1(1:N_Nodes); % Select the First Column
Q_T = reshape(List_1,R_Nodes,D_Nodes); % Convert List to Array (Requires Transposing)
Q = Q_T'; % Transpose

Risposta accettata

Chunru
Chunru il 27 Lug 2021
foldername = 'C:***FILE LOCATION***'; % True folder name needed
fileprefix = 'RESULTS_ONE_';
for i=1:N % N is number index to file name
% Combine foldername, fileprefix, and file number to form the full file
% name
fn = fullfile(foldername, sprintf('%s%d.txt', fileprefix, i));
fid1 = fopen(fn); % OPEN FILE
A = textscan(fid1,'%f %*f %*f %*f %*f'); % Read First Column
Col_1 = cell2mat(A); % Convert to List
List_1 = Col_1(1:N_Nodes); % Select the First Column
Q_T = reshape(List_1,R_Nodes,D_Nodes); % Convert List to Array (Requires Transposing)
Q = Q_T'; % Transpose
end
  4 Commenti
Stephen23
Stephen23 il 27 Lug 2021
@Joseff Parke: this answer is missing FCLOSE.
Having many open files will slow down your OS, slow down MATLAB, and can cause MATLAB to crash.
Joseff Parke
Joseff Parke il 27 Lug 2021
Hi Both,
Ok Brilliant thanks for your help, I'll include that too!

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 27 Lug 2021
Modificato: KSSV il 27 Lug 2021
txtFiles = dir('*.txt') ;
N = length(txtFiles) ;
for i = 1:N
thisFile = txtFiles(i).name ;
fid1 = fopen(thisFile); % OPEN FILE
A = textscan(fid1,'%f %*f %*f %*f %*f'); % Read First Column
Col_1 = cell2mat(A); % Convert to List
List_1 = Col_1(1:N_Nodes); % Select the First Column
Q_T = reshape(List_1,R_Nodes,D_Nodes); % Convert List to Array (Requires Transposing)
Q = Q_T'; % Transpose
fclose(fid1) ;
end
  1 Commento
Joseff Parke
Joseff Parke il 27 Lug 2021
Amazing thanks for your help, I accepted the other answer purely as there were more comments but yours was equally as helpful!

Accedi per commentare.

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by