For loop through multiple folders

5 visualizaciones (últimos 30 días)
OLav Røthe Kyte
OLav Røthe Kyte el 6 de Nov. de 2019
Editada: Ruger28 el 6 de Nov. de 2019
I am trying to loop through 34 folders with result from different boundary conditions, but have difficulties defining the path.
All i get in the command window is "C:\Users\OlavK\OneDrive - NTNU\NTNU\SIMA\Conditions\Condition1_101_1File D\results.txt could not open
Unable to read SIMO results"
file1={'1_101_1', '1_102_2', '2_103_1', '2_104_2', '3_105_1', '3_106_2', '4_107_1', '4_108_2', '5_109_1', '5_110_2', '5_201_1', '5_202_2', '5_203_1', '5_204_2', '6_111_1', '6_112_2', ...
'7_113_1', '7_114_2', '8_115_1', '8_116_2', '9_117_1', '9_118_2', '10_119_1', '10_120_2', '11_121_1', '11_122_2', '12_123_1', '12_124_2', '13_125_1', '13_126_2', '14_127_1', '14_128_2', ...
'15_129_1', '15_130_2'};
str=string(file1);
%% User-defined input
for i=1:length(str)
folder = fprintf('C:\\Users\\OlavK\\OneDrive - NTNU\\NTNU\\SIMA\\Conditions\\Condition%s',str(i));
prefix = 'sima';
hullBody = 1; % typically 1
numForChan = 123; % this is the number of channels in the force results ...
% storage. You can find this in the key_sima_elmfor.txt file.
%% READ SIMULATION OUTPUT
%==========================================================================
%%READ SIMULATION OUTPUT
% Read the SIMO results text file to get the channel names and number
% of steps
[nchan, nts, dt, chanNames] = readSIMO_resultstext([folder '\results.txt']);
% Read the binary file
AA = read_simoresults([folder '\results.tda'],nts);
sizeAA = size(AA);
if (sizeAA(1)<nts || nts<1); disp('Unable to read SIMO results'); return; end;
% Determine which channels to read for the platform motions, wave
% elevation
[chanMotions, chanWave] = getchannelNumbers(chanNames,hullBody);
if (chanMotions(1)<1 || chanWave<1); disp('Unable to read SIMO results'); return; end;
time_SIMO = AA(:,2);
  1 comentario
Bob Thompson
Bob Thompson el 6 de Nov. de 2019
I notice in your example that the last folder is 'Condition1_101_1File D,' but it seems to only be getting created from ['Condition',str(i)]. Where is the 'File D' portion coming from?

Iniciar sesión para comentar.

Respuestas (2)

Ruger28
Ruger28 el 6 de Nov. de 2019
Editada: Ruger28 el 6 de Nov. de 2019
file1={'1_101_1', '1_102_2', '2_103_1', '2_104_2', '3_105_1', '3_106_2', '4_107_1', '4_108_2', '5_109_1', '5_110_2', '5_201_1', '5_202_2', '5_203_1', '5_204_2', '6_111_1', '6_112_2', ...
'7_113_1', '7_114_2', '8_115_1', '8_116_2', '9_117_1', '9_118_2', '10_119_1', '10_120_2', '11_121_1', '11_122_2', '12_123_1', '12_124_2', '13_125_1', '13_126_2', '14_127_1', '14_128_2', ...
'15_129_1', '15_130_2'};
str=string(file1);
MainDirectory = 'C:\Users\OlavK\OneDrive - NTNU\NTNU\SIMA\Conditions'; % define a main directory
%% User-defined input
for i=1:length(str)
folder = fullfile(MainDirectory,strcat('Condition',str(i))); % use fullfile & strcat to create the current folder
fileName = fullfile(folder,'results.txt'); % use fullfile to generate the real filename
Now you can send this fileName into your functions, instead of
[folder '\results.txt']

Ruger28
Ruger28 el 6 de Nov. de 2019
Editada: Ruger28 el 6 de Nov. de 2019
For a cleaner approach (depending on your MATLAB version)...
MainDirectory = 'C:\Users\OlavK\OneDrive - NTNU\NTNU\SIMA\Conditions'; % define a main directory
ResultFiles = dir(fullfile(MainDirectory,'**/results.txt')); % recursivly seach through subfolders looking for results.txt files
for i = 1:length(ResultsFiles)
CurrentFile = fullfile(ResultFiles(i).folder,ResultFiles(i).name);
...etc
end

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by