MATLAB Answers

how to open,read ,scan, multiple files in a loop

20 views (last 30 days)
deepak chowdary gottipati
deepak chowdary gottipati on 18 Nov 2020
Answered: Manas Meena on 25 Mar 2021
I have an array of numbers for which the data should be readed from files.
But the data is present in multiple files . An algorithm which opens each file and search for the array number and assign the data when it finds the exact array number.
I have developed a code which reads only one file. If it is not available in first file it should select second file and read.
laenge_netz = 5 ; % Verteilung der L?nge der Fein-Vernetzung
breite_netz = 10 ; % Verteilung der Breite der Fein-Vernetzung
hoehe_netz = 5 ; % Verteilung der H?he der Fein-Vernetzung
Netz = [laenge_netz,breite_netz,hoehe_netz];
elem_CL = 100;
for hoehe = 1:Netz(3)
for laenge = 1:Netz(1)
for breite = 1:Netz(2)
Elem_CL{breite}(hoehe,laenge)= elem_CL; % Arrays mit den Elementennummern
elem_CL = elem_CL+1;
S_schub_CL = Elem_CL; % Array f?r Orthogonal-Schubspannungen, die sich in PLNSOLS**.lis-Daten befinden
S_hyd_CL = Elem_CL; % Array f?r hydrostatische Spannungen, die sich in PLNSOLNL**.lis-Daten befinden
E_volu_scheibe_CL = Elem_CL; % Array f?r Volumen der einzelnen ELemente, die sich in S**_ENVM.lis-Daten befinden
for scheibe = 1:Netz(2) % 15 Verteilungen
elem_h = 1; % horizontale Elementnummer
elem_v = 1; % vertikale Elementnummer
Fsel = strcat('CL_','PRESOLS',num2str(scheibe),'.LIS'); % Strings in horizontaler Richtung verketten
File = fopen(Fsel); % File ?ffnen
while ~feof(File)
Fscan = textscan(File,'%s %n %s'); % Text scannen, w?hrend feof(File)
h1 = Fscan{1}(1); % gescannter Wort in h1 speichern
h2 = 'ELEMENT='; % definieren h2
if strcmp(h1,h2) % beide Strings vergleichen
element = Fscan{1,2}; % Falls h1 = h2 ist, wird die Elementnummer gespeichert.
Ftext = fgetl(File); % Test lesen
Fscan = textscan(File,...
'%n %*n %*n %*n %n %*n %*n', 'CollectOutput', true); % Daten auslesen; 1.Reihe(Knotennr.), 5.Reihe(Schubspannung in XY-Ebene)
if element == S_schub_CL{scheibe}(elem_h,elem_v)
S_schub_CL{scheibe}(elem_h,elem_v) = Fscan{1}(5,2); % Spannung in 5. Zeile (location of the node); in 2. Reihe (bei 'Fscan')
elem_v = elem_v+1; % n?chste Elementnummer
if elem_v > Netz(1) % 24 Verteilungen
elem_h = elem_h+1; % n?chste Elementnumber
elem_v = 1; % Anfangswert
% pause;
%fprintf('check \n\n');
Ftext = fgetl(File);

Answers (1)

Manas Meena
Manas Meena on 25 Mar 2021
If i understand your question correctly you want to process multiple files in a loop, for this you can just use dir and read the files out of a folder.
S = dir('*.txt');
N = {};
Please refer to the following MATLAB answers links to achieve the same:

Community Treasure Hunt

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

Start Hunting!