Read xls files from differente folders and write on MATLAB

181 views Asked by At

I have a folders with this hierarchy :

Data meteo 
    Avril
         Day1
             file.xls
         Day2
             file.xls
         Day3
             file.xls
    May
         Day1
             file.xls
         Day2
             file.xls
         Day3
             file.xls
    June
         Day1
             file.xls
         Day2
             file.xls
         Day3
             file.xls

I need to read all files in those folders and choose just some columns to work with and write them in another directory with the same hierarchy.

I'm new on Matlab and I tried to test with this code.

D = 'data meteo';
DESTINATION = "data meteo destination"
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % number of subfolders of D.

for ii = 1:numel(N)
    T = dir(fullfile(D,N{ii},'*')); 
    C = {T([T.isdir]).name}; %
    for jj = 1:numel(C)
        myExcelFile = fullfile(D,N{ii},C{jj});

        %data = xlsread(myExcelFile);

        data1=xlsread(myExcelFile,'A:A');
    data2=xlsread(myExcelFile,'B:B');
    data3=xlsread(myExcelFile,'C:C');
    data4=xlsread(myExcelFile,'E:E');

    data=[data1 data2 data3 data4]

    %print(data)
    xlswrite(DESTINATION,fullfile(D,N{ii},C{jj}));

    end
end

I need to have result as the same hierarchy folders :

Data meteo Destination 
    Avril
         Day1
             file.xls 
         Day2
             file.xls
         Day3
             file.xls
    May
         Day1
             file.xls
         Day2
             file.xls
         Day3
             file.xls
    June
         Day1
             file.xls
         Day2
             file.xls
         Day3
             file.xls
1

There are 1 answers

0
Craig Arnold On

I've edited/expanded your code, and it seems to achieve what I think you're after when I try it on the fake directories I created for it, but see if it works for you (but I'd recommend testing it on a COPY of your data - in case something doesn't work and it changes/overwrites the original files).

The main changes I made were:

  • to add the setdiff function you used for N= also for C=, to ignore the '.' and '..' directories.

  • to change your code for myExcelFile to get the file itself rather than the 'Day1' directory

  • to add code that makes matching directories within your DESTINATION directory, to save the new files in.

D = 'data meteo';
DESTINATION = 'data meteo destination';
S = dir(fullfile(D,'*'));
N = setdiff({S([S.isdir]).name},{'.','..'}); % number of subfolders of D.

if ~isdir(DESTINATION)
    mkdir(DESTINATION)
end

for ii = 1:numel(N)
    T = dir(fullfile(D,N{ii},'*'));
    C = setdiff({T([T.isdir]).name},{'.','..'}); %

    if ~isdir(fullfile(DESTINATION,N{ii}))
        mkdir(fullfile(DESTINATION,N{ii}))
    end

    for jj = 1:numel(C)

        if ~isdir(fullfile(DESTINATION,N{ii},C{jj}))
            mkdir(fullfile(DESTINATION,N{ii},C{jj}))
        end

        fileList = dir(fullfile(D,N{ii},C{jj},'*.xls*'));
        myExcelFile = fullfile(D,N{ii},C{jj},fileList.name);

        data1=xlsread(myExcelFile,'A:A');
        data2=xlsread(myExcelFile,'B:B');
        data3=xlsread(myExcelFile,'C:C');
        data4=xlsread(myExcelFile,'E:E');

        data=[data1 data2 data3 data4];

        xlswrite(fullfile(DESTINATION,N{ii},C{jj},fileList.name),data);
    end
end