Using readtable to load excel files in a different folder

9 ビュー (過去 30 日間)
Noah Leavitt
Noah Leavitt 2019 年 7 月 25 日
編集済み: dpb 2022 年 2 月 20 日
I used dir to get the files names in a specified folder "Formatted_Excel" that exists within the working folder and that works just fine. But I need to use load the data from those files so I try using the readtable function, but MATLAB does not recognize that they exist. Does anyone know how to get around this?
The one tricky part is that sometimes MATLAB does recognize that they exist and it loads them, but it loads them from the earlier folder named "Excel" that also exists within the working folder. The folders both contain the same number and named files but the "Formatted_Excel" folder contains files that are all formatted the same thus making them easy to manipulate in matlab. Please someone help!
Heres my code:
%getting all of the data from folder 'Formatted_Excel' containing files for each participant
folder_read_in =dir('Formatted_Excel');
% getting names of all relevent files (last command is bc matlab thinks
% there are extra files in that folder for some reason)
files = {folder_read_in.name}; files = string(files(4:end));
P = readtable(files(1));
Thanks!

採用された回答

dpb
dpb 2019 年 7 月 25 日
folder_in='Formatted_Excel'; % directory of interest
d=dir(fullfile(folder_in,'*.xls*'); % return the .xls files in the given folder
for i=1:numel(d)
P=readtable(fullfile(folder_in,d(i).name);
...
% do whatever w/ the i-th file here before going on to the next...
...
end
  2 件のコメント
Yonatan Wiegner
Yonatan Wiegner 2022 年 2 月 20 日
Is it a way to that without the 'for' loop?
dpb
dpb 2022 年 2 月 20 日
編集済み: dpb 2022 年 2 月 20 日
Not in some form or fashion, no. readtable is not vectorized internally to handle more than one file at a time. Nor are any other of the MATLAB file i/o functions.
There is (or at least used to be, I presume it is still available there) a File Exchange m-file function FILEFUNCTION that provides a similar functionality for multiple files as do arrayfun or cellfun for numeric or cell arrays that provides a higher level of abstraction by moving the loop to a lower level, but it still consists of the for...end loop therein. Of course, the builtin functions are doing the same internally as well.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by