Using readtable to load excel files in a different folder

9 次查看(过去 30 天)
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 个评论
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 CenterFile Exchange 中查找有关 Loops 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