Reputation: 108
I'm fairly new at MATLAB, so forgive if I'm saying stuff that is completely wrong. I am trying to write a little program that reads in the files from a certain directory with extension .xlsx and then assigns the columns to vectors. When I manually give up the file names, my program looks like this:
files = {'130926','130927'}
file_number = 1;
file_amount = length(files);
while file_number <= file_amount
file_name = files(file_number);
cd('C:\place_where_I_store_my_files');
A = xlsread(char(strcat(file_name)));
J = A(:,1);
J_sp = A(:,2);
file_number = file_number + 1
end
I've tried a lot of different things to read in the files automatically among wich:
files = {'*.xlsx'}
But it all yields errors. Thanks for reading.
Matthias
Upvotes: 1
Views: 1075
Reputation: 108
FYI, this did it for me:
files = dir('C:\Users\Matthias\Desktop\KUL\2e_Master\Thesis\MBR_data\MBR_online\*cs.xlsx')
file_number = 1;
file_amount = length(files);
while file_number <= file_amount
file_name = files(file_number).name;
cd('C:\place_where_I_store_my_files');
A = xlsread(char(file_name));
J = A(:,1);
J_sp = A(:,2);
file_number = file_number + 1
end
Greetings, Matthias
Upvotes: 0
Reputation: 47794
How about this :-
>> list = dir('C:\path\*.xlsx');
>> names=cellfun(@(x)x(1:end-5),{list.name},'UniformOutput', false);
Access using names{1}
, names{2}
... so on
This creates an anonymous function that works on cells of names received from dir
command.
Type : help dir
and help cellfun
for more details at command prompt
Upvotes: 0
Reputation: 109119
Use the dir
function with a wildcard search.
my_path = 'C:\place_where_I_store_my_files';
xlsfiles = dir(fullfile(my_path, '*.xlsx'));
for ii = 1 : length(xlsfiles)
disp(fullfile(my_path, xlsfiles(ii).name));
end
The above code will display the names of all xlsx files in the directory you specify.
Upvotes: 1