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

4 次查看(过去 30 天)
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];
disp(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;
end
end
end
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
end
else
fclose(File);
% pause;
%fprintf('check \n\n');
end
else
Ftext = fgetl(File);
end
end
%fclose(File);
end
clear('File','Fsel','Fscan','Ftext','elem_h','elem_v','scheibe','element','laenge','breite','hoehe','h1','h2')

回答(1 个)

Manas Meena
Manas Meena 2021-3-25
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 = {S.name};
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!