Reading Multiple excel files with different file names

51 views (last 30 days)
Paj
Paj on 12 Dec 2017
Commented: Rik on 31 Oct 2018
I have this bit of code where i'm indexing the location of places of interest in every country in the world:
[~, ~, raw] = xlsread('C:\Users\paroo\Desktop\Filtered\Afganistan.xlsx','Sheet1','A2:P69');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[1,2,3,4,5,6,7,9,10,13,14,15,16]));
stringVectors(ismissing(stringVectors)) = '';
raw = raw(:,[8,11,12]);
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
Afganistan.point = stringVectors(:,8);
Now i have a directory with multiple excel file (200) named by country. Can matlab load each filename separately ? been trying this so far (doesnt work) (sourced from MATLAB answers):
D = dir(['C:\Users\paroo\Desktop\Filtered', '\*.xlsx']);
filenames = {D(:).name}.';
data = cell(length(D),1);
for ii = length(D):-1:1
% Create the full file name and partial filename
fullname = ['C:\Users\paroo\Desktop\Filtered' D(ii).name];
% Read in the data
data{ii} = xlsread(fullname);
end

Accepted Answer

Rik
Rik on 12 Dec 2017
D = dir(['C:\Users\paroo\Desktop\Filtered', '\*.xlsx']);
filenames = {D(:).name}.';
data = cell(length(D),1);
for ii = length(D):-1:1
% Create the full file name and partial filename
fullname = ['C:\Users\paroo\Desktop\Filtered\' D(ii).name];
% Read in the data
data{ii} = xlsread(fullname);
end
Don't forget the filesep between the folder and the name.
  6 Comments
Rik
Rik on 31 Oct 2018
I can't reproduce this error. Are you sure the sheet name has a space?
What is the full error (all red text)? I don't really see what cell would be converted to a double in this code.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!