load files from two different folders in matlab

1k views Asked by At

Hi and I am little bit new in matlab. I have two different folders in my laptop, each one contains about 400 different files. I want to load all these files (400 from first folder and 400 from second folder), I tried like that but doesn't work:

folder1=('E:\results\1\'); 
folder2=('E:\results\2\'); 
data=load([folder1,'*.m']);
data1=load([folder2,'*.m']);

and then I want to take first file from folder1 and subtracted from first file from folder1 and save it in new folder. and do that for all other files ... etc can some expert give me any suggerstion!! thanks in advance.

1

There are 1 answers

2
macduff On

Pretty sure load takes one file at a time. Try a simple variant like this:

folder1=('E:\results\1\'); 
folder2=('E:\results\2\');
files1 = dir( [folder1,'*.m'] );
files2 = dir( [folder2,'*.m'] );

data = cell(length(files1),1);  % I don't know what's in the mat files, but let's start with a cell array
data1 = cell(length(files2),1);
for ii=1:length(files1)
  data{ii} = load(fullfile(folder1,files1(ii).name));
end
for ii=1:length(files2)
  data1{ii} = load(fullfile(folder2,files2(ii).name));
end

There are other, more one liner ways, but this is a fairly pedantic.