Trouble reading 6 excel files (with multiple sheets) and storing each excel as a its own matrix with my loop.
조회 수: 2 (최근 30일)
이전 댓글 표시
Here is my current code
%My Code
foldername = 'My Folder Path';
files = dir(fullfile(foldername, '*.xlsx'))
for i = 1:length(files)
data = xlsread(fullfile(foldername,files(i).name));
end
This code works in some respect, however data only seems to hold 1 sheet from 1 excel file, despite the files variable having all 6 files in a 6x1 struct.
Ideally what I would like is 6 separate variables (or one that stores all six files, where I can reference specific files) in my workspace.
Then, I want to be able to reference data{sheets{columns/rows across every sheet}}. I believe I can do this part, its more so getting past the hurdle of importing each excel file.
Hope that makes sense and someone can offer some advice. Thank you.
댓글 수: 1
Elias Gule
2018년 3월 29일
you are overwriting the values stored in the variable "data" in that for loop. So the data stored in the variable will be from the last file that was processed.
답변 (2개)
Elias Gule
2018년 3월 29일
Doing this might help: Replacing
data = xlsread(fullfile(foldername,files(i).name));
with
data{i} = xlsread(fullfile(foldername,files(i).name));
where data{i} contains data from the ith file.
Elias Gule
2018년 3월 29일
ok, lets do this then:
foldername = 'My Folder Path';
files = dir(fullfile(foldername, '*.xlsx'))
for i = 1:length(files)
xldata = xlsread(fullfile(foldername,files(i).name));
data{i} = xldata;
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!