I am a beginner in MATLAB coding so I have taken code from else-ware to apply it to my own needs. I've so far managed to get all files from one folder into a column vector, but now I want to get all files from all folders within a parent directory into this single column vector. here's my code:
...
folder = ('parent_directory_path_name\01');
files = eval(['dir(''' folder '\*wind*.na'')']); % take files with wind in name
N = length(files);
%%
for n=1:N
filename = files(n).name;
eval(['fid = fopen(''' folder '/' filename ''');'])
data=textscan(fid, '%s','delimiter','\n');
lines=data{1};
lines=lines(56:end);
for i=1:size(lines,1)
[s(i).time s(i).east s(i).north] = strread(lines{i},'%f %f %f %*f %*f %*f %*f %*f');
end
time = [s.time]';
east (:,n) = [s.east]';
north(:,n) = [s.north]';
fclose(fid);
end
%%
ea = east (:); % put matrix columns into 1 column
no = north (:);
...
I'm sure there must be a simple loop I can put around the folder specification, but I can't work it out. I've also looked at: How to get all files under a specific directory in MATLAB? , but as the method is very different I'm unsure how to apply this to the code I'm using.
Any tips would be much appreciated.
Thanks, Luke
EDIT
Re:Shai, (comments section not big enough)
Okay, I've given it a try but I don't get how to open the files now. Here's what I've tried:
sub_f = dir( fullfile( 'parent_folder', '*' ) );
for si = 1:numel( sub_f )
if sub_f(si).name(1) =='.', continue;
end; % skip '.' and '..'
files = dir( fullfile( 'parent_folder', sub_f(si).name, '*wind*.na' ) ); % get all files in sub folder
for n = 1:numel(files)
% put your code here...
filename = files(n).name;
fid = fopen('' sub_f '/' filename '');
data=textscan(fid, '%s','delimiter','\n');
lines=data{1};
lines=lines(56:end);
for i=1:size(lines,1)
[s(i).time s(i).east s(i).north] = strread(lines{i},'%f %f %f %*f %*f %*f %*f %*f');
end
time = [s.time]';
east (:,n) = [s.east]';
north(:,n) = [s.north]';
fclose(fid);
end
end
Thanks again!