Extract ¨multiple .xls file from a folder?

2 ビュー (過去 30 日間)
Prathamesh Halagi
Prathamesh Halagi 2021 年 1 月 12 日
編集済み: Prathamesh Halagi 2021 年 1 月 24 日
Hello,
I am looking to extract 215 .xls files from a folder. I want to also extract specific columns from these files. The colums in all these files are same. I am using the follwing code to extract one file. Is there any way to extract all the files from this folder?
opts = detectImportOptions('1.xls', 'Sheet','cycle', 'Range', 'A1:C21', 'PreserveVariableNames', true);
opts.SelectedVariableNames = opts.SelectedVariableNames([1, 2, 3]);
Table_2 = readtable('1.xls', opts);
--
Thank you.
  1 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 1 月 12 日
hello
simple suggestion below. of course you can use readtable instead of the (older) xlsread - but chek speed if it's important to you; xlsread still ok for simple tasks.
also check if files are sorted according to your wishes - you may have to sort them if you have file names ending like file_1, file_10 etc...
range = 'C2:D10'; % Read a specific range of data:
file_list = dir('*.xlsx'); % list all xlsx files in current directory
for i = 1:length(file_list)
filename = file_list(i).name;
data_in{i} = xlsread(filename,range); % stores the filename data in array of cells
% insert your code here
end

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

採用された回答

Ive J
Ive J 2021 年 1 月 12 日
If you want to extract the content of all your .xls files within a certain directory at once, you can do it as:
% assuming you're already in the target directory
ds = spreadsheetDatastore(pwd, 'Sheets', 'cycle', 'Range', 'A1:C25', 'FileExtensions', '.xls', 'ReadVariableNames', true);
ds = readall(ds); % aggregates all contents as a single table
  1 件のコメント
Prathamesh Halagi
Prathamesh Halagi 2021 年 1 月 12 日
編集済み: Prathamesh Halagi 2021 年 1 月 24 日
That works for me well. I am getting proper results. Thanks a lot

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

その他の回答 (0 件)

カテゴリ

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