1

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!

Community
  • 1
  • 1
Luke
  • 13
  • 1
  • 4
  • you should replace the line `filename=filse(n).name` with the following line: `filename = fullfile('parent_folder',sub_f(si).name,files(n).name)` – Shai Dec 17 '12 at 13:32
  • Thanks again. I still can't get it to work, but i've done it manually for now. I will come back to it later when I need to do this for more folders. Cheers – Luke Dec 17 '12 at 14:09
  • it would help if you'd be more specific when you say "can't get it to work": what is the error? where does it fails? – Shai Dec 17 '12 at 14:17
  • Please don't feel obliged to answer as I should really spend some time learning about this myself. But if you like here's the error: Error: File: script Line: ... Unexpected MATLAB expression. corresponding to `(fid = fopen('' sub_f '/' filename '');)` I think it's the '/' that doesn't work as I thought – Luke Dec 17 '12 at 14:34
  • @Shai please see above comment – Luke Dec 17 '12 at 14:41
  • After changing the `filename` line, you need to change the `fopen` line to : `fid = fopen( filename );` – Shai Dec 17 '12 at 15:04

1 Answers1

1

A few comments:

  1. instead of concatenating folder names and file names as simple strings, a better practice would be to use fullfile command:

    folder = fullfile( 'parent_folder', '01' );

  2. You do not need the cumbersome evel expressions:

    files = dir( fullfile( folder, '*wind*.na' ) );

  3. For iterating over sub folders you can simply do

    
    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...
        end
    end
    
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thanks for your help. I couldn't get this to work so have done it manually this time as I only have 12 folders. – Luke Dec 17 '12 at 13:21
  • @user1909613 - please see my comment to your edited question. – Shai Dec 17 '12 at 13:32