Reading Multiple excel files with different file names

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

 採用された回答

Rik
Rik 2017 年 12 月 12 日

1 投票

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 件のコメント

Stephen23
Stephen23 2017 年 12 月 12 日
編集済み: Stephen23 2017 年 12 月 12 日
"Don't forget the filesep between the folder and the name."
That is why we recommend using fullfile.
Rik
Rik 2017 年 12 月 14 日
If you found this answer useful, please mark it as accepted answer. It will give me reputation points and will make it easier for other people with the same question to find an answer. If not, please comment with what problems you are still having.
Kah Boon Ho
Kah Boon Ho 2018 年 10 月 29 日
@Rik The script is works. Thanks Rik. If there is a case I would like to read sheet2 and sheet4 and etc in an excel sheet, what is the command that I should add it in? I am only able to read the sheet1 of the excel sheet from the above command.
Rik
Rik 2018 年 10 月 29 日
If you read the xlsread doc you can see the syntax with which you can specify the sheet (you can even use the sheet number, though the sheet name is safer).
Kah Boon Ho
Kah Boon Ho 2018 年 10 月 31 日
編集済み: Rik 2018 年 10 月 31 日
Thanks Rik. After try it, the comment stated 'Conversion to cell from double is not possible.' Therefore unable to read the sheet 2 and so on. Anything I do wrongly here? My script as below:
data{ii} = xlsread(fullname,'Sheet 2');
The previous script I follow as per your original script
Rik
Rik 2018 年 10 月 31 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

製品

質問済み:

Paj
2017 年 12 月 12 日

コメント済み:

Rik
2018 年 10 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by