Please... I want to import several excel files at once. used for_fuction

1 回表示 (過去 30 日間)
JIN-HWA JEONG
JIN-HWA JEONG 2016 年 5 月 2 日
コメント済み: CS Researcher 2016 年 5 月 2 日
I want to import several excel files at once. used for_fuction
I used this code.
--------------------------------------------------------------
source_dir = 'C:/Users/Cju/Documents/MATLAB/a/' ;
dest_dir = '/C:/Users/Cju/Documents/MATLAB/a' ;
source_files = dir(fullfile(source_dir, '*.xlsx'));
for i = 1:length(source_files)
data = xlsread(fullfile(source_dir, source_files(i).name));
end
--------------------------------------------------------------
In 'a'folder, there are 5 excel files.
But, Imported just first file.
Is there '*.xlsx' problem?
What is wrong?
This problem is very important for me.
Please.. Answer

回答 (2 件)

CS Researcher
CS Researcher 2016 年 5 月 2 日
編集済み: CS Researcher 2016 年 5 月 2 日
You can try this:
oldfolder = cd(source_dir);
xlfiles = dir('*.xlsx');
nfiles = length(xlfiles);
for i = 1:nfiles
% Access files by xlfiles(i).name (No need to use fullfile here)
end
cd(oldfolder);
  3 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 2 日
You will need to use fullfile() if the files are not in the current folder.
CS Researcher
CS Researcher 2016 年 5 月 2 日
I did not know this. Thanks for sharing the link.

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


Image Analyst
Image Analyst 2016 年 5 月 2 日
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F for reading in a sequence of xlsx files.
If you want to save all the arrays, then you will have to index data and make it a cell array (in case all workbooks don't have the same size matrices in them), so in your loop:
fullFileName = fullfile(source_dir, source_files(i).name);
data{i} = xlsread(fullFileName); % Save in one cell of a cell array.
See thet FAQ on cell arrays if you don't feel completely comfortable with them.

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by