Using readtable to load excel files in a different folder
9 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
dpb
on 25 Jul 2019
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 Comments
dpb
on 20 Feb 2022
Edited: dpb
on 20 Feb 2022
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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!